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, shell scans the command line once, finds the structure of (CMD), executes CMD in (CMD) once, gets its standard output, and then puts the 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.
If ((16#5f)) = 95 (16#5f)
③ The variable value can also be redefined simply by (()), such as a=5; ((a++)) redefines $a to 6
$i-lt = $i-lt = $i-lt = $i-lt = $i-lt = $i-lt = $i-lt = $i-lt
[] 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, if [[a!= 1&&a!= 2]] can be used directly, or if [a-ne 1\] &&[a!= 2] or if [
a -ne 1 -a
A – 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
Curly braces {} 1
(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} and
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}. for
{var:=string} substitution rule and
Var :=string replaces {var:-string} with {var:-string}
{var:=string} If var is empty, replace it with 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:? String} If var is not empty, the value of var is used to replace it
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 the left side (on the keyboard # is to the left of $) % is to remove the right side (on the keyboard % is to the right of $) # and % are the minimum match, two identical symbols are the maximum match. ${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 modes changes the value of variable, which is used only in pattern
When matching symbols, % and %%, # and ## are distinguishable. Pattern in the structure supports wildcards,
Represents 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 parenthesized with a space after a colon or a number, as in {var: -2},
1-3} {var: 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
** (1) ** (1) ** (1) ** (1) {a} The value of the variable A, where braces can be omitted without causing ambiguity.
(2) (CMD) command replacement, the effect is the same as’ CMD ‘, the result is the shell command CMD, some shell versions do not support (CMD) command replacement, the effect is the same as’ CMD ‘, the result is the shell command CMD replacement, some shell versions do not support (CMD) command replacement, The result is the shell command CMD. Some shell versions do not support the () command substitution, such as TCSH.
(3) $((expression)) has the same effect as exprexpression. It calculates the value of mathematical expression exp, which can be computed as long as exp conforms to the operation rules of C language, even the ternary operators and logical expressions.
5. Run 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.