Special characters in Bash

Source: Internet
Author: User

# indicates a comment

#! Specifies the parser for the current script

#!/bin/bash

echo "Hello World"

; Command delimiter

#!/bin/bash

Echo Hello;echo there

filename=ttt.sh

If [-R "$filename"]; Then

echo "File $filename exists."; CP $filename $filename. bak

Else

echo "File $filename not found."; Touch $filename

Fi echo "File test Complete"

;; Terminate case option

#!/bin/bash

Varname=b

Case ' $varbname ' in

[A-z]) echo "ABC";;

[0-9]) echo "123";;

Esac

. Equivalent to the source command. The source command is used to read and execute commands in filename.sh under the current bash environment

$ source test.sh #等价于. test.sh

"String will block the interpretation of most of the special characters in Stirign

' String will block the interpretation of all special characters in string, more strongly than

/filename path delimiter, also used as division arithmetic operator

\ A single-character reference mechanism, \x is escaped into character X

' Command replace the command structure to assign the output of the commands to another variable. "will take precedence.
$ cp ' mkdir bak ' test.sh bak #先创建bak目录, then copy test.sh into Bak

: null command, equivalent to NOP (no option). is also considered to have the same effect as the shell's built-in command. : is a bash built-in command, and its exit code is 0

#!/bin/bash

While:

Do

echo "Endless Loop"

Done

#!/bin/bash

Condition=5

If [$condition-GT 0]

Then: #什么都不做, exit the branch

Else

echo "$condition"

Fi

: When used in conjunction with the redirect operator >, a file is emptied, but the file's permissions are not modified. If the previous file does not exist, create the file.

$: > test.sh #test. SH was emptied.

: When used in conjunction with the redirect operator >>, it will not have any effect on the pre-existing target file, and if the file does not exist before, the file is created.

: It can also be used to annotate the business not to turn off error checking, but also to make delimiters in the/ect/passwd and $path variables.

? In a double-brace structure,? is the ternary operator of the C language

#!/bin/bash

a=10

((T=a<50?8:9))

Echo $t

$ variable substitution, command substitution

$ CD $ (echo Documents)

$ pwd

(()) command group: The list of commands in parentheses will be run as a child shell. The variables in parentheses are not available for the remainder of the script because they are in the child shell. The parent process, the script itself, is not able to read variables created in the child process, that is, variables created in the shell.

#!/bin/bash

A = 123

(a = 321;) #括号中的变量a相当于一个局部变量

echo "a = $a"

Bash test.sh #output a = 123

(()) can also initialize an array

#!/bin/bash

Arr= (1 2 3 4 5 6) #不可以出现空格哦 ~ Arr =: No, it's not.

Echo ${arr[3]}

{} file name extension

#!/bin/bash

if [!-W ' t.txt '];

Then

Touch T.txt

Fi

echo ' Test txt ' >> t.txt

CP T.{txt,back}

{{}} can also represent code fast, also known as an internal group, which is actually an anonymous function.

#!/bin/bash

A=123

{a=321;} #中间一定要有空格哦 ~

echo "a = $a"

$ bash test.sh #output a = 321 variable declared in #在 {}, which is visible for code in other parts of the script

[] Condition test, [is part of the shell built-in test command, not a link to external commands in/urs/bin/test

#!/bin/bash

A=5

If [$a-lt 10]

Then

echo "A: $a"

Else

echo "a > 10"

Fi

[] array element, in the context of an array structure, used to refer to the number of each element in the array.

#!/bin/bash

Arr= (12 22 32)

arr[0]=2

Echo ${arr[0]}

<> redirection

test.sh > FileName #重定向test the output of the. sh to the file filename. If filename exists, it will be overwritten.

test.sh &> filename #重定向test. sh stdout and stderr to filename

test.sh >&2 #重定向test. Sh stdout to stderr

test.sh >> filename #把test. SH output is appended to the file filename. If filename does not exist, it will be created

| Pipeline, analyzes the output of the front command, and outputs the output as input to the command behind it.

$ PS-EF | Aux Tomcat

-option, prefix, in all commands, if you want to use parameters, precede them with-

#!/bin/bash

A=5

B=5

If ["$a"-eq "$b"]

Then

echo "A is equal to B"

Fi

-For redirecting stdin or Stdou

Back up all modified files in the current directory for the last 24 hours

#!/bin/bash

backupfile=backup-$ (date +%m-%d-%y) #在备份文件中插入时间

archive=${1:-$BACKUPFILE}

Tar cvf-' find. -mtime-1-type f-print ' > $archive. Tar

Gzip $archive. Tar

echo "Directory $PWD backed up in archive file \" $archive. tar.gz\ "."

Exit 0

~ Indicates home directory

 

  

Special characters in Bash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.