The parentheses in shell have their special usage, which are summarized as follows:
1. brackets after the symbol $
The value of $ {A} variable A can be omitted without ambiguity.
$ (CMD) command is replaced and the result is the output of Shell Command cmd, which has the same effect as 'cmd'. However, some shell versions do not support command replacement in the form of $ (), such as tcsh.
$ (Exp) and 'expr exp 'have the same effect. Calculate the number of exp in the mathematical expression. The exp must comply with the c algorithm rules, even the Three-object operator and logical expression can be computed.
2. Execute multiple commands
(Cmd1; cmd2; cmd3) open a sub-shell to execute commands cmd1, cmd2, and cmd3 in sequence. Separate the commands with semicolons. The last command can have no semicolons.
{Cmd1; cmd2; cmd3;} execute commands cmd1, cmd2, and cmd3 in the Current Shell sequence. Separate the commands with semicolons. The last command must be followed by a semicolon, the first command and the left parenthesis must be separated by spaces.
For {} and (), the redirection character in brackets only affects this command, while the redirection character outside brackets affects all commands in brackets.
3. Special Use of double brackets
() Enhances the usage of parentheses, which is often used for comparison of arithmetic operations. variables in double brackets can be prefixed with the $ symbol, as long as the expressions in the brackets comply with the C language operation rules, multiple expressions can be separated by commas.
For example, you can directly use for (I = 0; I <5; I ++, for I in 'seq 0 4' or for I in {0 .. 4 }.
For example, you can directly use if ($ I <5). If double parentheses are not used, the value is if [$ I-lt 5].
[[] [Enhanced square brackets] are commonly used for string comparison. They are mainly used for conditional testing. Expressions in double brackets can use C language syntaxes such as &, |,<,>.
For example, you can directly use if [[$! = 1 & $! = 2]. If double parentheses are not applicable, the parameter is if [$ A-ne 1] & [$! = 2] Or if [$ A-ne 1-A $! = 2].