Parentheses in the shell (parentheses, brackets, curly braces)

Source: Internet
Author: User
Tags arithmetic

One, parenthesis, bracket ()

1, single parenthesis ()

① Command Group. The commands in parentheses will be executed 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, executes 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. This 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, if the result of the expression is 0, the exit status code returned is 1, or false, and a non-0-valued expression returns an exit status code of 0, or "true". If the logical judgment, the expression exp is true is 1, false is 0.

② as long as the operators and expressions in parentheses conform to the C-language arithmetic rules, they can be used in $ (exp) or even in the trinocular operator. For different carry (such as binary, octal, hex) operations, the output is automatically converted to decimal. such as: Echo $ ((16#5f)) result is 95 (16 decimal)

③ (()) can also redefine variable values, such as a=5; ((a++)) to redefine $a to 6

④ the variable in double brackets can not use the $ symbol prefix. Multiple expressions are supported in parentheses, separated by commas.

[python] view plaincopy

  1. if   ($i < )   

  2. if 5  ]  

  3. if 1 2  ]  

  4. if 1 ] &&  [  $a  !=   ]  

  5. if [[$a! = 1 && $a! = 2 ]]

  6. for  i  in  $ (seq    );d o echo  $i;d one  

  7. for  i  in   ';d o echo  $i;d one  

  8. for   ((i= ;i< ;i++));d o echo  $i;d one  

  9. for  i  in 0. };d o echo  $i;d one  

II) brackets, square brackets []1. single brackets []

①bash's internal command, [and test is equivalent. If we don't specify the absolute path, we usually use Bash's own commands. The left bracket in the if/test structure is the command ID that calls test, and the right bracket is judged by the close condition. This command takes its arguments as a comparison expression or as a file test, and returns an exit status code based on the results of the comparison. 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 [] are only = = and! =, both are used for string comparisons and are not available for integer comparisons, and integer comparisons can only be used in the form of-EQ,-GT. The greater than sign is not supported either for string comparisons or for integer comparisons. If you really want to use, you can use the escape form for string comparisons, if you compare "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 a regular expression to describe 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. There is no filename extension or word splitting between all characters in [[and]], but parameter extensions and command substitution occur.

② supports pattern matching of strings, and even supports the shell's regular expressions when using the =~ operator. String comparisons can be made to the right as a pattern, not just a string, such as [[Hello = = Hell]], the result is true. Matches a string or wildcard character in [[]] without the need for quotation marks.

③ using [[...]] The conditional judgment structure, rather than [...], can prevent many logic errors in the script. For example,,&&, | |, <, and > operators can normally exist in the [[]] conditional judgment structure, but if they appear in the [] structure, an error will be found.

④bash the expression in double brackets as a separate element and returns an exit status code.

Three) curly braces, curly braces {}1, general usage.

① Curly brace expansion. (wildcard (globbing)) expands the file name in curly braces. In curly braces, white space is not allowed, unless the whitespace is referenced or escaped. First: Expand the comma-delimited list of files in curly braces. such as touch {a,b}.txt result is a.txt b.txt. The second type: in curly braces with dots (.. The sequential file list of the splits expands, for example: touch {A. D}.txt results for a.txt b.txt c.txt d.txt

[python] view plaincopy

  1. Bogon:/home/bash # ls {ex1,ex2}.sh   

  2. Ex1.sh ex2.sh

  3. Bogon:/home/bash # ls {ex{1..3},ex4}.sh

  4. ex1.sh ex2.sh ex3.sh ex4.sh

  5. Bogon:/home/bash # ls {ex[1-3],ex4}.sh

  6. ex1.sh ex2.sh ex3.sh ex4.sh

The ② code block, also known as an internal group, actually creates an anonymous function. Unlike the commands in parentheses, the commands in curly braces do 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 the {}.

2) Several special replacement structures:${var:-string},${var:+string},${var:=string},${var:?string}

A,${var:-string} and ${var:=string}: If the variable var is empty, replace the ${var:-string} with string in the command line, or the variable var is not empty, then replace the value of the variable VARs ${var:-string 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, Assigning a string to a variable var: ${var:=string} A common use is to determine whether a variable is assigned a value or not, and assign a default value to it.
B. ${var:+string} The substitution rule is the opposite of the above, that is, only when Var is not empty to replace the string, if Var is empty, then do not replace or substitute variable var value, that is, null value. (because the variable var is empty at this time, the two statements are equivalent)
The c,${var:?string} substitution rule is: if the var variable is not empty, replace ${var:?string} with the value of the variable Var, and if the variable var is null, the string is output to the standard error and exited 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, the string is not necessarily a constant value, the value of another variable or the output of a command can be used.

  3) Four pattern matching replacement structures:${var%pattern},${var%%pattern},${var#pattern},${var# #pattern}

The first mode: ${variable%pattern}, in this mode, the shell looks in the variable to see if it gives the pattern end, if so, remove the contents of the variable from the command line to the shortest matching mode on the right
Second mode: ${variable%%pattern}, in this mode, the shell looks in the variable to see if it is a given pattern end, if so, remove the contents of the variable from the command line to the longest matching pattern on the right
Third mode: ${variable#pattern} In this mode, the shell looks in the variable to see if it starts with a given pattern, and if so, removes the shortest matching pattern from the command line to the left of the variable
Fourth mode: ${variable# #pattern} In this mode, the shell looks in the variable to see if it is a given pattern end, and if so, removes the variable from the command line with the longest matching pattern on the right.
None of these four modes will change the value of variable, where only the * match symbol is used in pattern,% and percent, #和 # #才有区别. The pattern in the structure supports wildcards, * denotes 0 or more arbitrary characters, which represent 0 or one arbitrary character, [...] Matches the characters inside the brackets, [!...] Indicates a mismatch between the characters in brackets

Iv. parentheses after the symbol $

(1) The value of ${a} variable A, which can be omitted without causing ambiguity.

(2) $ (cmd) command substitution, and the ' cmd ' effect is the same, the result is shell command cmd's output, and some shell versions do not support the form of a $ () command substitution, such as tcsh.

(3) $ (expression) and ' exprexpression ' have the same effect, calculating the numeric value of the mathematical expression exp, where exp can be computed as long as it conforms to the rules of the C language, even the three-mesh operator and the logical expression.

v. Use of

1. more than one command execution

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

(2) Canda brackets,{cmd1;cmd2;cmd3;} executes the 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 parentheses.


Parentheses in the shell (parentheses, brackets, curly braces)

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.