Special Use of shell brackets

Source: Internet
Author: User

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].

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.