Functions of various parentheses in Shell

Source: Internet
Author: User
When writing shell scripts, brackets and brackets are often used. I. Parentheses, Parentheses () 1. Single parentheses. ① Command Group. The commands in parentheses will open a new sub-shell for sequential execution, so the variables in parentheses cannot be used by the rest of the script. Multiple commands in parentheses are separated by semicolons. The last command can have no semicolons, and there is no space between each command and the parentheses. ② Replace the command. Equivalent to 'cmd', shell scans the command line once and finds the $ (CMD) structure. Then, it executes CMD in $ (CMD) once to obtain its standard output, then place the output to the original command. Some shells are not supported, such as tcsh. ③ Used to initialize an array. For example, array = (a B c d) 2. Double parentheses. ① Integer extension. This type of extended computation is an integer computation and does not support floating point computation. (Exp) structure extension and calculation of the value of an arithmetic expression, if the expression result is 0, then the returned exit status code is 1, or "false ", the exit status code returned by a non-zero expression is 0 or "true ". If the expression exp is true, it is 1, and false is 0. ② As long as the operators and expressions in parentheses comply with the C language operation rules, they can be used in $ (exp), or even the three-object operator. When performing different carry operations (such as binary, octal, and hexadecimal), all output results are automatically converted to decimal. For example, Echo $(16 # 5f) results in 95 (16-carried to decimal) ③ variable values can also be redefined simply with (), such as a = 5; (A ++) You can redefine $ A as a variable in 6 ④ double brackets without the $ symbol prefix. Multiple Expressions can be separated by commas. 2) brackets, square brackets [] 1. Single brackets. ① Bash Internal commands, [equivalent to test. If we do not need to specify the absolute path, we usually use Bash commands. The left brackets in the IF/test structure are the command IDs that call test, and the right brackets are used to close the condition judgment. This command uses its parameters as a comparison expression or as a file test, and returns an exit status code based on the comparison result. The IF/test structure does not require right brackets, but this is required in the new version of bash. ② The comparison operators available in test and [] are only = and! =, Both are used for string comparison and cannot be used for integer comparison. Integer comparison can only be in the form of-EQ or-GT. Both string comparison and integer comparison do not support numbers greater than or less than numbers. If you really want to use it, you can use escape form for string comparison. If you compare "AB" and "BC": [AB \ <BC], the result is true, that is, the return status is 0. The logic and logic in [] can be expressed by-A and-o. ③ Character range. Used as part of a regular expression to describe a matched character range. Regular Expressions cannot be used in brackets for test purposes. ④ In the context of an array structure, brackets are used to reference the numbers of each element in the array. 2. Double brackets. ① [[It is a keyword of bash programming language. It is not a command. The [[] structure is more common than the [] structure. All characters between [and] are not subject to file name extension or word segmentation, but parameter extension and command replacement occur. ② Supports string pattern matching, using = ~ It even supports regular expressions of shell. When comparing strings, you can use the right side as a pattern, not just a string, such as [[Hello = Hell? ], And the result is true. [[] Matches strings or wildcards without quotation marks. ③ Using the [[...] condition to determine the structure, rather than [...], can prevent many logical errors in the script. For example, the &, |, <, and> operators can normally exist in the [[] condition judgment structure, but if they appear in the [] structure, an error is returned. ④ Bash regards the expression in double brackets as a separate element and returns an exit code. 3) braces and curly braces {} 1. General usage. ① Expanded in braces. (Globbing) will expand the file name in braces. White spaces are not allowed in braces unless the white space is referenced or escaped. First, expand the comma-separated file list in braces. For example, if you set "Touch platform A", the result of "bits. txt" is a.txt B .txt. Type 2: Use a dot (...) in braces (..) split the list of sequential files to expand, for example, touch into a..d;.txt is the.txt B .txt c.txt d.txt code block, also known as an internal group. This structure actually creates an anonymous function. Unlike the commands in parentheses, the commands in braces do not start a new sub-shell, that is, the remaining part of the script can still use the variable in parentheses. The commands in parentheses are separated by semicolons. The last one must also contain semicolons. There must be a space between the first command of {} and the left parenthesis. 2. Special usage. For example: $ {var:-string}, $ {var # */}, and $ {var %. *}, $ {var: 5: 5}, and so on. 4) Example: If ($ I <5)
If [$ I-lt 5]
If [$ A-ne 1-A $! = 2]
If [$ A-ne 1] & [$! = 2]
If [[$! = 1 & $! = 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 <5I ++); Do echo $ I; done
For I in {0 .. 4}; do echo $ I; done can see the above example. Various parentheses and usage can be solved in several different ways, but while saving code, we also need to take care of the readability and portability of the script, and try to use less risky and portable scripts as much as possible. Don't just worry about it and develop a good code style.

Functions of various parentheses in Shell

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.