## parentheses ()
1. Single parenthesis ()
① Command group. Commands in parentheses will be executed sequentially with a new subshell, so variables in parentheses cannot be used by the rest of the script. Multiple commands in parentheses are separated by semicolons (;). The last command may not have a semicolon (;). There is no space between each command and the parentheses.
② Command replacement. Equivalent to ‘CMD’, the shell scans the command line and finds
(CMD) structure, then will
(CMD) structure, then execute the CMD in (CMD) once, get its standard output, and then put this output in the original command. Some shells don’t support it, such as TCSH.
③ Used to initialize an array. Array =(a b c d)
2. Double parentheses (())
① Integer expansion. This extended calculation is an integer calculation and does not support floating point. The ((exp)) structure extends and evaluates the value of an arithmetic expression. If the result of the expression is 0, the exit status code returned is 1, or “false”, while an expression with a non-zero value returns either 0, or “true”. The expression exp is 1 if it is true and 0 if it is false.
② As long as the operators and expressions in parentheses conform to the operation rules of C language, they can be used in
(exp)), or even the ternary operator. When you do different carry operations (binary, octal, hexadecimal), the output is all automatically converted to decimal. Such as: echo
(exp)), or even the ternary operator. When you do different carry operations (binary, octal, hexadecimal), the output is all automatically converted to decimal. Echo ((16#5f))
③ The variable value can also be redefined simply by (()), such as a=5; ((a++)) redefines $a to 6
④ Is often used to compare arithmetic operations. Variables in double brackets may not be used
Symbol prefix. Multiple expressions in parentheses can be separated by commas. You can use for((I =0; i<5; I ++)), for I in ‘seq 0 4’ or for I in {0.. 4}. If (()
Symbol prefix. Multiple expressions in parentheses can be separated by commas. You can use for((I =0; i<5; I++)), foriin ‘seq04’ or foriin0.. 4. If ((I <5)); if [$i-lt 5];
## # []
1. Single brackets []
The if/test structure uses the left parenthesis to identify the command that called test, and the right parenthesis to close the condition. This command takes its arguments as comparison expressions or file tests, and returns an exit status code based on the result of the comparison. Closing brackets is not mandatory in the if/test structure, but newer versions of Bash require it.
②Test and [] == and! =, both are used for string comparison, not integer comparison, integer comparison can only use the form -eq, -gt. Neither string nor integer comparisons support greater than or less than signs. If you really want to, you can use an escape form for string comparisons. If you compare “ab” with “BC” : [ab \< BC], the result is true, and the return status is 0. The logic and logic in [] may be represented by -a and -o.
③ Character range. Used as part of a regular expression to describe a range of matched characters. The re cannot be used inside brackets used for test.
④ In the context of an array structure, brackets are used to refer to the number of each element in the array.
2. Double brackets [[]]
[[is a key word in the Bash programming language. It is not a command, and the [[]] construct is more generic than the [] construct. All characters between [[and]] do not have filename extension or word splitting, but argument extension and command substitution.
② Supports string pattern matching, and even supports shell regular expressions when using the =~ operator. String comparisons can use the one on the right as a pattern, not just a string, such as [[hello == hell?]], which is true. [[]] matches a string or wildcard character without quotes.
③ Use the […]] condition judgment structure instead of […] Can prevent many logic errors in scripts. For example, &&, | |, <, and > operator can judge condition of normal exists in [[]] structure, but if the words appear in the structure of [], complains. For example, you can use if [[a!= 1&& A!= 2]. If double parentheses are not applicable, you can use if [A-ne 1\] && \[A!= 2] or if [A-ne 1-AA − NE1 − AA!= 2].
④ Bash treats the expression inside the double braces as a single element and returns an exit status code.
Example:
if ($i<5) if [ $i -lt 5 ] if [ $a -ne 1 -a $a != 2 ] if [ $a -ne 1] && [ $a != 2 ] if [[ $a != 1 && $a ! = 2 ]] for i in $(seq 0 4); do echo $i; done for i in `seq 0 4`; do echo $i; done for ((i=0; i<5; i++)); do echo $i; done for i in {0.. 4}; do echo $i; doneCopy the code
## ## ## ##
1. General usage
(1) Expand braces. Globbing will extend the filename in braces. In braces, no whitespace is allowed unless the whitespace is referenced or escaped. First: extend the comma-separated list of files in braces. For example, touch {a,b}. TXT. The result is A.txt b.txt. The second kind: the curly braces with dot (..) The split sequential file list is extended, for example: touch {a.. TXT result is a.txt b.txt c.txt d.txt
# ls {ex1,ex2}.sh ex1.sh ex2.sh # ls {ex{1.. 3},ex4}.sh ex1.sh ex2.sh ex3.sh ex4.sh # ls {ex[1-3],ex4}.sh ex1.sh ex2.sh ex3.sh ex4.shCopy the code
② A code block, also known as an inner group, actually creates an anonymous function. Unlike commands in braces, commands in braces do not open a new subshell to run, meaning that the variables in braces can still be used for the rest of the script. The commands in parentheses are separated by a semicolon, and the last command must also have a semicolon. There must be a space between the first command of {} and the open parenthesis.
2. Several special replacement structures
${var:-string},${var:+string},${var:=string},${var:? string}Copy the code
1.
{var:-string}var:− String and {var:=string}: If the var variable is empty, it is replaced by string in the command line
{var:-string}. If var is not empty, the value of var is used to replace it
Var :−string. If var is not empty, the value of var is used to replace {var:-string}. Var :=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} Var :=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string} {var:=string}
${var:+string} ${var:+string} ${var:+string} ${var:+string} ${var:+string} (Since the variable var is null, the two statements are equivalent.)
(3) {var:? If var is not empty, replace var with the value of var:? If var is not empty, replace {var:? With var’s value. String}; If the var variable is empty, string is printed to standard error and exits from the script. We can use this feature to check that the value of a variable is set.
Supplementary extension: String does not have to be constant in the above five substitution constructs; it can be the value of another variable or the output of a command.
3. The four modes match the replacement structure
Pattern matching memory method:
# is to remove a single symbol from the left (on the keyboard # on the left), % is to remove a single symbol from the right (on the keyboard % on the left), # and % are minimum matches, and two identical symbols are maximum matches.
${var%pattern},${var%%pattern},${var#pattern},${var##pattern}
Copy the code
${variable%pattern} : ${variable%pattern} : ${variable%pattern} : ${variable%pattern} : ${variable%pattern
${variable%%pattern}. In this pattern, the shell looks in the variable to see if it has a pattern ending. If it does, the shell removes the longest pattern from the variable from the command line
${variable#pattern} When using this pattern, the shell looks in variable to see if it starts with the given pattern. If so, the shell removes from the command line the contents of variable with the shortest pattern on the left
${variable##pattern} When using this pattern, the shell looks in variable to see if it has a pattern ending. If it does, the shell removes the longest pattern from variable from the command line
None of the four patterns changes the value of variable, where % and %%, # and ## are different only when the * matching symbol is used in pattern. Pattern in the pattern structure supports wildcards, with * indicating zero or more arbitrary characters,? Matches only one arbitrary character, […] Matches the characters inside the brackets, [!… Does not match the characters inside the brackets.
# var=testcase # echo $var testcase # echo ${var%s*e} testca # echo $var testcase # echo ${var%%s*e} te # echo ${var#? e} stcase # echo ${var##? e} stcase # echo ${var##*e} # echo ${var##*s} e # echo ${var##test} caseCopy the code
4. String extraction and replacement
${var:num},${var:num1:num2},${var/pattern/pattern},${var//pattern/pattern}
Copy the code
The first mode:
{var:num}, in this mode, the shell extracts the num character from var to the end of all characters. If num is positive, start at 0 on the left; If num is negative, the string is extracted from the right, but must be parenthesized with a colon followed by a space or a number or the entire num, as in
Var :num. In this mode, the shell extracts the num character from var to the end of all characters. If num is positive, start at 0 on the left; If num is negative, the string is extracted from the right, but must be followed by a space after a colon or a number or the entire num in parentheses, such as {var: -2}, {var:1-3} or var:1−3 or {var: -2)}.
The second mode:
{var:num1:num2}, num1 is the position, num2 is the length. Says from the
Var :num1:num2, where num1 is the position and num2 is the length. Represents the first digit from the var string
Num1, the length of the initial extraction is
Num1 starts extracting substrings of length num2. It can’t be negative.
The third pattern: ${var/pattern/pattern} replaces the first pattern matching the var string with another pattern.
${var//pattern/pattern} replaces all matching patterns in the var string with another pattern.
[root@centos ~]# var=/home/centos
[root@centos ~]# echo $var
/home/centos
[root@centos ~]# echo ${var:5}
/centos
[root@centos ~]# echo ${var: -6}
centos
[root@centos ~]# echo ${var:(-6)}
centos
[root@centos ~]# echo ${var:1:4}
home
[root@centos ~]# echo ${var/o/h}
/hhme/centos
[root@centos ~]# echo ${var//o/h}
/hhme/cenths
Copy the code
## ## ##
${a} ${a} ${a} ${a} ${a} ${a}
(2) (CMD) command replacement, the same effect as’ CMD ‘, the result is the shell command CMD, some shell versions do not support (CMD) command replacement, the same effect as’ CMD ‘, the result is the shell command CMD, Some Shell versions do not support command substitution in the form of (), such as TCSH.
(3) $((expression)) has the same effect as’ exprexpression ‘. It computes the value of mathematical expression exp, which can even be computed as long as exp conforms to the operation rules of C language.
## 5. Use
Executing multiple Commands
(1) single parenthesis, (cmd1; cmd2; Cmd3) Open a new subshell and run the commands cmd1,cmd2, and cmd3 in sequence, separated by a semicolon. The last command may not have a semicolon.
(2) single curly braces, {cmd1; cmd2; cmd3; } Execute commands cmd1,cmd2,cmd3 in the current shell order, separated by a semicolon, followed by a semicolon, and separated by a space between the first command and the open parenthesis.
For {} and (), redirects in parentheses affect only that command, while redirects outside parentheses affect all commands in parentheses.