Tips Summary:
String comparison with double brackets [[]]; arithmetic comparison with single brackets []--left and right spaces
Arithmetic operations with double parentheses (()); Shell commands and output parentheses ()--left and right without spaces
Quick Replace with curly braces {}--left and right blank lattice
Anti-single quotes play the role of command substitution
Single bracket ():
Open a command group-the contents of the parentheses open a child shell to run independently, the parentheses are concatenated with semicolons, the last command is not required, and the commands and parentheses have no spaces
Get command output--a=$ (commands), equivalent to a=$ command
, get command output passed to variable a
Initializes an array of--array= (a B c d)
Double brackets (()):
Omit the arithmetic operation of the $ symbol--for ((i=0;i<5;i++)); if ($i <5); a=5; ((a++)) The $a can be redefined to 6, and multiple expressions are supported in parentheses to separate commas.
C-Language rule operation--$ ((exp)), Exp is an operator that conforms to the C language rule, expression
Step into the system operation-binary, octal, hexadecimal operation, the output is automatically converted to decimal. such as: Echo $ ((16#5f)) result is 95 (16 decimal)
Single brackets []:
string comparison--== and! =
Integer comparison-Not equal to:-GT: Greater than;-lt: less than;-eq: equals;-ne
Array index--array[0]
double brackets [[]]:
String comparison--you can put the right side as a pattern, not just a string, such as [[Hello = = Hell]], the result is true. Matches a string or wildcard character in [[]] without the need for quotation marks.
Logical operators--to prevent many logic errors in scripting, such as,&&, | |, < and > operators can normally exist in the [[]] conditional judgment structure, but if it appears in the [] structure, it will be an error. For example, you can use if [[$a! = 1 && $a! = 2]], if you do not apply double brackets, if [$a-ne 1] && [$a! = 2] or if [$a-ne 1-a $a! = 2] 。
Exit code--bash The expression in double brackets as a separate element and returns an exit status code.
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);d o echo $i;d one
For i in seq 0 4
;d o echo $i;d one
For ((i=0;i<5;i++));d o echo $i;d one
For i in {0..4};d o echo $i;d one
Curly braces {}:
Create an anonymous function--no new process is opened, and the remaining variables in parentheses are still available. The commands in parentheses are separated by semicolons, and the last one must have semicolons. There must be a space between the first command and the opening parenthesis of the {}.
Special Replacement--${var:-string},${var:+string},${var:=string},${var:?string}
①${var:-string}和${var:=string}:若变量var为空,则用在命令行中用string来替换${var:-string},否则变量var不为空时,则用变量var的值来替换${var:-string};不同之处是${var:=string}常用于判断var是否赋值,没有的话则给var赋上一个默认值。 ② ${var:+string}:替换规则和上面的相反,即只有当var不是空的时候才替换成string,若var为空时则不替换或者说是替换成变量 var的值,即空值。(因为变量var此时为空,所以这两种说法是等价的) ③${var:?string}:替换规则为:若变量var不为空,则用变量var的值来替换${var:?string};若变量var为空,则把string输出到标准错误中,并从脚本中退出。我们可利用此特性来检查是否设置了变量的值。
Note: In these five alternative structures, the string is not necessarily a constant value, the value of another variable or the output of a command can be used.
Pattern match replace--${var%pattern},${var%%pattern},${var#pattern},${var# #pattern}
# is to remove the left (on the keyboard # on the left side);% is to remove the right (on the keyboard% on the right side); The single symbol in #和% is the minimum match, and the two same symbol is the maximum match.
First mode: ${variable%pattern}. The shell looks in variable to see if it gives the pattern end, and if it does, remove the variable to the shortest matching pattern on the right.
Second mode: ${variable%%pattern}, in this mode, the shell looks in the variable, see if it gives the pattern end, if it is, remove the variable to the right of the longest matching pattern
Third mode: ${variable#pattern} In this mode, the shell looks in the variable to see if it starts with a given pattern pattern, and if so, removes the shortest matching pattern on the left side of the variable.
Fourth mode: ${variable# #pattern} In this mode, the shell looks in the variable to see if it gives the pattern end, if so, to remove the longest matching pattern on the left side of the variable
None of these four modes will change the value of the variable, where only the match symbol is used in pattern, % and percent, #和 # #才有区别. The pattern in the structure supports wildcards, which represent 0 or more arbitrary characters, which represent only one arbitrary character, [...] Matches the characters inside the brackets, [!...] Indicates a mismatch between the characters in the brackets.
# Var=testcase
# echo $var
TestCase
# echo ${var%se}
Testca
# echo $var
TestCase
# echo ${var%%se}
Te
# echo ${var#?e}
Stcase
# echo ${var##?e}
Stcase
# echo ${var##*e} * *
*# echo ${var##s}
E
# echo ${var# #test} * *
Case
String extraction and substitution--${var:num},${var:num1:num2},${var/pattern/pattern},${var//pattern/pattern}
First mode: ${var:num},shell extracts all characters from Num to the end in Var. If NUM is positive, start at the left 0, and if NUM is negative, extract the string from the right, but you must use a space after the colon or a number or the entire num with parentheses, such as ${var:-2}, ${var:1-3}, or ${var: (-2)}.
The second mode: ${VAR:NUM1:NUM2},NUM1 is the position, num2 is the length. Represents starting from the $NUM1 position of the $var string to extract a substring of length $num2. cannot be a negative number.
A third mode: ${var/pattern/pattern} means replacing the first matching pattern of the Var string with another pattern.
The fourth mode: ${var//pattern/pattern} indicates that all matching patterns in the Var string are replaced with another pattern.
[Email protected] ~]# Var=/home/centos
[Email protected] ~]# echo $var
/home/centos
[[email protected] ~]# echo ${var:5}
/centos
[[email protected] ~]# echo ${var:-6}
Centos
[[email protected] ~]# echo ${var: (-6)}
Centos
[[email protected] ~]# echo ${var:1:4}
Home
[[email protected] ~]# echo ${var/o/h}
/hhme/centos
[[email protected] ~]# echo ${var//o/h}
/hhme/cenths
for {} and (), the redirection in parentheses affects only that command, and the redirection outside the brackets affects all the commands in parentheses.
The parentheses after the symbol $
(1) The value of ${a} variable A, which can be omitted without causing ambiguity.
(2) $ (cmd) command substitution, and the cmd
effect is the same, the result is the shell command cmd of the output, some shell version does not support the form of $ () command substitution, such as tcsh.
(3) $ (expression) and the exprexpression
same effect, calculates the numeric value of the mathematical expression exp, where exp can be computed as long as it conforms to the rules of the C language, even three-mesh operators and logical expressions.
The role of various parentheses (), (()), [], [[]], {} in the shell