This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation.
Various Brackets in Shell and Their Functions: (), ((())), [], [[]], {}
Category Programming Techniques
I. Parentheses, Round Brackets ()
1. Single Parentheses ()
Command Group. Commands within the parentheses will be executed in a new subshell in sequence, so variables within the parentheses cannot be used by the rest of the script. Multiple commands within the parentheses are separated by semicolons, the last command can be without a semicolon, and there is no need for spaces between the commands and the parentheses.
Command Substitution. Equivalent to
cmd
, the shell scans the command line, finds the $(cmd) structure, executes the cmd within $(cmd) once, obtains its standard output, and then places this output in the original command. Some shells do not support this, such as tcsh.Used for Initializing Arrays. For example: array=(a b c d)
2. Double Parentheses (( ))
Integer Expansion. This type of expansion calculates integer arithmetic and does not support floating-point types. The ((exp)) structure expands and calculates the value of an arithmetic expression, if the result of the expression is 0, then the returned exit status code is 1, or "false," while a non-zero value expression returns an exit status code of 0, or "true." If it is a logical judgment, the expression exp is true for 1, and false for 0.
As long as the operators and expressions within the parentheses comply with C language arithmetic rules, they can all be used in $((exp)), even the ternary operator. When performing different base (such as binary, octal, hexadecimal) operations, the output results are automatically converted into decimal. For example: echo $((16#5f)) results in 95 (hexadecimal to decimal conversion).
Simply using (( )) can also redefine the value of a variable, for example, a=5; ((a++)) can redefine $a as 6.
Commonly used for arithmetic operation comparisons, variables within double parentheses do not need to be prefixed with the $ symbol. Multiple expressions within the parentheses are supported, separated by commas. As long as the expressions within the parentheses comply with C language arithmetic rules, for example, you can directly use for((i=0;i<5;i++)), if double parentheses are not used, it would be for i in
seq 0 4
or for i in {0..4}. Similarly, you can directly use if (($i<5)), if double parentheses are not used, it would be if [ $i -lt 5 ].
II. Square Brackets []
1. Single Square Bracket []
It is an internal command of bash, [ is equivalent to test. If we do not specify an absolute path, what we usually use is the command that comes with bash. The left square bracket in the if/test structure is the command identifier for calling test, and the right square bracket closes the condition judgment. This command takes its arguments as a comparison expression or as a file test, and returns an exit status code based on the result of the comparison. The right square bracket is not mandatory in the if/test structure, but it is required in newer versions of Bash.
The only comparison operators available in test and [] are == and !=, both of which are used for string comparison and cannot be used for integer comparison, which can only be done in the form of -eq, -gt, etc. Neither string comparison nor integer comparison supports greater than or less than signs. If you really want to use them, for string comparison, you can use the escaped form, such as comparing "ab" and "bc": [ ab < bc ], the result is true, which means the return status is 0. Logical AND and OR in [ ] are represented by -a and -o.
Character range. Used as part of a regular expression, describing a range of characters to match. Square brackets used for test purposes cannot use regular expressions.
In the context of an array structure, square brackets are used to reference the index of each element in the array.
2. Double Square Brackets [[]]
[[ is a keyword in the bash programming language. It is not a command, the [[ ]] structure is more versatile than the [ ] structure. All characters between [[ and ]] do not undergo filename expansion or word splitting, but do undergo parameter expansion and command substitution.
Supports pattern matching of strings, and even supports shell regular expressions when using the =~ operator. When comparing strings, the right side can be a pattern, not just a string, for example, [root@centos ~]# echo ${var//o/h} /hhme/cenths
Four, the parentheses following the symbol $
(1) ${a} The value of variable a, the curly braces can be omitted when it does not cause ambiguity.
(2) $(cmd) Command substitution, has the same effect as
cmd
, the result is the output of the shell command cmd. Some shell versions do not support the $() form of command substitution, such as tcsh.(3) $((expression)) Same as
exprexpression
, calculates the numerical value of the mathematical expression exp, where exp complies with the operational rules of the C language, and even the ternary operator and logical expressions can be calculated.
Five, Usage
-
1. Execution of Multiple Commands
(1) Single parentheses, (cmd1;cmd2;cmd3) A new subshell is opened to execute the commands cmd1, cmd2, cmd3 in sequence, separated by semicolons, and the last command can be without a semicolon.
(2) Single curly braces, { cmd1;cmd2;cmd3;} The commands cmd1, cmd2, cmd3 are executed in sequence in the current shell, separated by semicolons, the last command must have a semicolon, and there must be a space between the first command and the left brace.
For {}
and ()
, the redirection symbols inside the parentheses only affect that command, while the redirection symbols outside the parentheses affect all the commands inside the parentheses.
>
Original text from: http://blog.csdn.net/taiyang1987912/article/details/39551385