The role of various parentheses in the shell (), (()), [], [[]], {}

Source: Internet
Author: User
Tags arithmetic

One, parenthesis, parentheses ()1, single parenthesis ()?? ① Command Group. The commands in parentheses will run in a new sub-shell sequence. So the variables in parentheses cannot be used by the rest of the script. Multiple commands in parentheses are separated by semicolons, and the last command can have no semicolon, and there is no space between the commands and the parentheses.

?? The ② command is replaced. Equivalent to ' cmd ', the shell scans the command line once, discovers the $ (CMD) structure, runs the cmd in $ (cmd) once, obtains its standard output, and then places the output in the original command.

Some shells are not supported. such as Tcsh.?? The ③ is used to initialize the array. such as: Array= (a b c d)? 2, double parenthesis (())?? ① integer extension. Such an extended calculation is an integer-type calculation and does not support floating-point types. (exp) structure expands and computes the value of an arithmetic expression. Assuming that the result of the expression is 0, the exit status code returned is 1, or false, and a non-0-value expression returns an exit status code of 0, or "true".

If logical inference, the expression exp is true is 1, false is 0. ? ? ② only the operators in parentheses, expressions that conform to the C-language arithmetic rules, can be used in $ (exp), or even a trinocular operator.

For different carry (such as binary, octal, hex) operations, the output is converted into decimal. such as: Echo $ ((16#5f)) result is 95 (16 decimal)?? ③ Simple (()) can also redefine variable values, such as a=5; ((a++)) $a can be redefined to 6?? ④ are often used in arithmetic operations, and variables in double brackets can be used without the $ symbol prefix. Multiple expressions are supported in parentheses, separated by commas. only the expressions in parentheses conform to the C-language arithmetic rules, For example, you can use the for ((i=0;i<5;i++)) directly, assuming that you do not use double brackets, either for i in ' seq 0 4 ' or for I in {0..4}.

If you can use the IF (($i <5)) directly, if you do not use double brackets, then if [$i-lt 5].


two, brackets, square brackets [] ? 1, single brackets []?? Internal command of the ①bash. [and test are equivalent.] Let's say we don't use absolute paths to indicate, usually we're using Bash's own commands. The left bracket in the if/test structure is the command ID that invokes test. The right bracket is inferred from the off condition. This command takes its parameters as a comparison or as a file test, and returns an exit status code based on the comparative results. The if/test structure does not have to be in the right bracket, but this is required in the new bash.

? ? The comparison operators available in ②test and [] have only = = and! =, both of which are used for string comparison, not for integer comparison, and for integers that can only use-eq. -gt such a form.

The greater-than sign is not supported, whether it is a string or an integer. Assuming that you really want to use the escape form for a string comparison, suppose the comparison is "AB" and "BC": [AB \< BC]. The result is true, that is, the return status is 0. Logic and logic in [] or using-A and-o are represented.?? ③ the character range. Used as part of the regular form. Describes a matching range of characters. The regular is not used within brackets as a test purpose.

? ? ④ in the context of an array structure, brackets are used to refer to the number of each element in the array. ? 2, double brackets [[]]?? ①[[is a key word for the Bash programming language. is not a command, [[]] structure is more general than [] structure.

The file name extension or Word cut does not occur for all characters between [[and]], but the parameter extension and command substitution occur. ② supports pattern matching for strings. Using the =~ operator even supports the Shell's regular form.

Strings can be compared to the right as a pattern, not just a string. For example [[Hello = = Hell]], the result is true.

Matches a string or wildcard character in

[[]]. No citation required.?? ③ using [[...]] The conditional inference structure. Instead of [...]. To prevent many logic errors in the script. Example. The &&, | |, <, and > operators can normally exist in the [[]] conditional inference structure, but assume that the present [] structure will give an error. For example, you can directly use if [[$a! = 1 && $a! = 2]], assuming that you do not use double brackets, if [$a-ne 1] && [$a! = 2] or if [$a-ne 1-a $a ! = 2]. ? ? ④bash the expression in double brackets as a separate element. and returns an exit status code.
example:
[CPP] view plaincopy

    1. if? ($i <5)????
    2. if? [$i?-lt?5?]????
    3. if? [? $a?-ne?1?-a? $a?! =?2?]????
    4. if? [? $a?-ne?1]?&&? [? $a?! =?2?]????
    5. if? [? $a?! =?1?&&, $a?! =?2]]????
    6. ?????
    7. for i?in?$ (seq?0?4); do echo? $i;d One????
    8. for i?in? ' Seq?0?4 '; do echo? $i;d One????
    9. for? ((i=0;i<5;i++)); do echo? $i;d One????
    10. for? I?in? {0..4}; do echo? $i;d One????
Three, curly braces, curly braces {}1. General use Method? ? ① Curly brace expansion. (wildcard (globbing)) expands the file name in curly braces. In curly braces, do not agree to have blanks unless the whitespace is referenced or escaped. The first: Expand the list of comma-cut files in curly braces. such as touch {a,b}.txt result is a.txt b.txt. The other one: in curly braces with dots (.. The sequential file list of the cuts expands, for example: touch {A. D}.txt results for a.txt b.txt c.txt d.txt [CPP]View Plaincopy
    1. #?ls? {ex1,ex2}.sh????
    2. Ex1.sh?? ex2.sh????
    3. #?ls? {ex{1..3},ex4}.sh????
    4. Ex1.sh?? Ex2.sh?? Ex3.sh?? ex4.sh????
    5. #?ls? {ex[1-3],ex4}.sh????
    6. Ex1.sh?? Ex2.sh?? Ex3.sh?? ex4.sh????
? ? ② code block. Also known as an internal group, this structure actually creates an anonymous function?

Unlike the commands in parentheses, the command inside the curly braces does not run with a new child shell, meaning that the remainder of the script can still be used in parentheses. 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 {}. ? 2, a few special replacement structure ${var:-string},${var:+string},${var:=string},${var:?

String}??? ①${var:-string} and ${var:=string}: If the variable var is empty, use string in the command line to replace the ${var:-string}. Otherwise, the variable var is not empty. Replace ${var:-string} with the value of the variable var. The substitution rule for ${var:=string} is the same as ${var:-string}, and the difference is ${var:=string} if Var is empty, replace ${var:=string} with string at the same time, Assign a string to a variable var: ${var:=string} A use method that is not used frequently is to infer whether a variable is assigned a value. If not, assign a default value to it.
? ? ? The substitution rule for ②${var:+string} is the opposite of the above, that is, only when Var is not empty, it is replaced with a string, and if Var is empty, it is not replaced or substituted for the variable var value. The value is null.

(because variable var is now empty.) So these two statements are equivalent)?
? ? ? ③${var:?

The string} substitution rule is: If the variable var is not NULL, then replace ${var:?string} with the value of the variable var, if the variable var is empty. The string is output to a standard error. and exit from the script.

We can use this attribute to check if the value of the variable is set.
? ? ? Supplemental extensions: In the above five alternative structures, string is not necessarily a constant value. You can use the value of another variable or the output of a command.3. Four pattern matching replacement structure${var%pattern},${var%%pattern},${var#pattern},${var# #pattern}??? The first mode: ${variable%pattern}, when such a pattern is found in the shell in variable. See if it gives the pattern end, assuming it is, remove the contents of the variable from the command line to the shortest matching pattern on the right.
???? Another mode: ${variable%%pattern}, such a pattern when the shell looks in the variable to see if it is a given pattern end, assuming that Remove the variable from the command line to the longest matching pattern on the right.
??? The third mode: ${variable#pattern} when such a pattern. The shell looks in variable to see if it is a given pattern, assuming that it removes the variable from the command line to the shortest matching pattern on the left.
??? Fourth mode: ${variable# #pattern} When such a pattern. The shell looks in variable to see if it is a given pattern end, assuming it is. Remove the variable from the command line to the longest matching pattern on the right.
in these four modes, the value of variable is not changed, only when the * match symbol is used in pattern. % and Percent, #和 # #才有差别.

The pattern in the structure supports wildcards, * denotes 0 or more random characters.

Indicates that only one random character is matched. [...] Matches the characters inside the brackets. [!...] Indicates a mismatch between the characters in the brackets .


[CPP] view plaincopy

    1. #?var=testcase?? ?
    2. #?echo $var?? ??
    3. testcase????
    4. #?echo?${var%s*e}? ??
    5. TESTCA????
    6. #?echo $var?? ??
    7. testcase???
    8. #?echo?${var%%s*e}? ??
    9. te??
    10. #?echo?${var#?e}?? ??
    11. stcase??
    12. #?echo?${var##?e}?? ??
    13. stcase??
    14. #?echo?${var##*e}?? ??
    15. ??
    16. #?echo?${var##*s}?? ??
    17. e????
    18. #?echo?${var# #test}?? ??
    19. case ????
Iv. parentheses after the symbol $

(1) The value of ${a} variable A, which can omit curly braces without causing ambiguity.

(2) The $ (cmd) command is replaced, and the ' cmd ' effect is the same. As a result, the shell command cmd is lost, and some shell version numbers do not support the $ () Form of command substitution, such as tcsh.

(3) $ (expression) and the ' exprexpression ' effect Similarly, calculates the numeric value of the mathematical expression exp, in which exp only conforms to the arithmetic rules of the C language, and even the three-mesh operator and the logical expression can be computed.


v. Use of

1, more than one command to run

(1) Single parenthesis, (CMD1;CMD2;CMD3) Open a new sub-shell Order Run command cmd1,cmd2,cmd3, each command separated by semicolons, the last command can be no semicolon .

(2) Canda brackets, {cmd1;cmd2;cmd3;} runs command cmd1,cmd2,cmd3 in the current shell order, separated by semicolons, and the last command must have a semicolon, separated by a space between the first command and the opening parenthesis.


for {} and (), the redirection in parentheses affects only that command, and the redirection outside the brackets affects all the commands in the parentheses.


What is the comparison of the size of the numbers?-eq? equals. -gt? greater than. -lt,-le, less than or equal to,-ge, greater than equals. -ne? Not equal to

The role of various parentheses in the 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.