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

Source: Internet
Author: User
one, parentheses, bracket () 1. Single bracket ()① 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 no spaces between the commands and parentheses. ② command replacement. Equivalent to ' cmd ', Shell scan the command line, found the $ (CMD) structure, will be $ (cmd) in the cmd execution, get its standard output, and then put the output to the original command.     Some shells are not supported, such as TCSH. ③ is used to initialize the array. such as: Array= (a b c d) 2. Double bracket (())① 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 expression evaluates to 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. ② can be used in $ ((exp)), or even as a three-mesh operator, as long as the operator in parentheses, the expression conforms to the C-language operation rule. For different carry (such as binary, octal, hexadecimal) operations, the output is automatically converted to the decimal system. For example: Echo $ ((16#5f)) results in 95 (16 decimal decimal) ③ simple (()) can also redefine variable values, such as a=5; ((a++)) You can redefine $a to 6④ a variable in double brackets without using the $ symbol prefix. Multiple expressions are supported in parentheses separated by commas.
if ($i <5)
if [$i-lt 5]
if [$a-ne 1-a $a!= 2]
if [$a-ne 1] && [$a!= 2]
if [[$a!= 1 && $a!= 2]] for
 
I and $ (SEQ 0 4);d o echo $i;d one for
i in ' seq 0 4 ';d o echo $i;d one for (
i=0;i< 5;i++);d o echo $i;d one for
i in {0..4};d o echo $i;d one
two) brackets, square brackets [] 1, single bracket []①bash's internal command, [is equivalent to test. 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 invokes test, and the right middle bracket is judged by the closing condition. This command takes its arguments as a comparison expression or as a file test, and returns an exit status code based on the result of the comparison.     The if/test structure does not have to be in the right bracket, but it is required in the new version of Bash. The comparison operators available in ②test and [] are only = = and!=, both used for string comparisons, not for integer comparisons, and integer comparisons can only be used in the form of-EQ,-GT. The greater-than sign is not supported for both string comparisons and integer comparisons. If you really want to use, for string comparisons you can use the escape form, if you compare "AB" and "BC": [AB \< BC], the result is true, that is, the return state is 0.     The logic and logic in [] or the use-A and-o representations. ③ character Range. Used as part of a regular expression to describe a matching range of characters.     You cannot use regular in brackets for test purposes. ④ 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 the key word for Bash programming languages. is not a command, the [[]] structure is more generic than the [] structure.     All characters between [[and]] do not have file name extensions or Word splits, but parameter extensions and command substitution occur. ② supports pattern matching of strings, and even supports shell regular expressions when using the =~ operator. String comparisons can take the right side as a pattern, not just a string, such as [[Hello = = Hell]], and the result is true.     [[]] matches a string or wildcard character and does not require quotation marks. ③ use [[...]] Conditional judgment structures, rather than [...], can prevent many logical errors in the script.     For example,,&&, | |, < and > operators can normally exist in the [[]] conditional judgment structure, but if they appear in the [] structure, the error occurs. ④bash the expression in double brackets as a separate element and returns an exit status code. three braces, curly braces {} 1. General usage. ① curly braces expand. (globbing) expands the file name in the curly braces. In curly braces, whitespace is not allowed unless the whitespace is referenced or escaped. First: Expand the comma-separated list of files in curly braces. such as touch {a,b}.txt result is a.txt b.txt. Second: In curly braces with dots (.. The split sequence file list expands, such as: touch {A. D}.txt result is a.txt b.txt c.txt d.txt
Bogon:/home/bash # ls {ex1,ex2}.sh
ex1.sh  ex2.sh
bogon:/home/bash # ls {ex{1..3},ex4}.sh
ex1.sh  ex2.sh  ex3.sh  ex4.sh
bogon:/home/bash # ls {ex[1-3],ex4}.sh  ex1.sh ex2.sh ex3.sh ex4.sh
The ② code block, also known as the inner group, actually creates an anonymous function. Unlike the commands in parentheses, the commands in curly braces do not run with a new child shell, that is, the remainder of the script can still use the variable in parentheses. The commands in parentheses are separated by semicolons, and the last one must also have a semicolon. There must be a space between the first command and the opening parenthesis of {}. 2) Several special substitution 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 a string in the command line, or the variable var is not empty, replace the value of the variable var ${var:-string The replacement rule for ${var:=string} is the same as ${var:-string}, but the difference is ${var:=string} if Var is empty, replace ${var:=string with string Assigning a string to variable var: ${var:=string} is a common use to determine whether a variable is assigned a value, or to assign it a default value if it is not.
The substitution rule for B. ${var:+string} is the opposite of the above, that is, if Var is not empty, it is replaced with a string, and if Var is empty, it is not replaced or substituted for the value of VAR, or null value. (because variable var is empty at this time, the two statements are equivalent)
The c,${var:?string} substitution rule is: If the variable var is not empty, replace the ${var:?string with the value of the variable Var, and if the variable var is empty, the string is output to the standard error and exits from the script. We can use this attribute to check whether the value of the variable is set.
Supplemental extension: In these five substitution structures, string is not necessarily constant, and can be used for 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}, this mode, the shell in the variable look up, see whether it gives the pattern at the end, if it is, from the command line to remove the contents of the variable the shortest matching pattern on the right
The second mode: ${variable%%pattern}, when this mode, the shell looks in the variable to see if it gives the pattern at the end, and 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 pattern patterns, and if so, remove the contents of the variable from the command line to the shortest matching pattern on the left.
Fourth mode: ${variable# #pattern} This mode, the shell in the variable look up, see whether it gives the pattern of the end, if it is, from the command line to remove the contents of the variable the longest matching pattern on the right
None of these four modes will change the value of the variable, in which% and percent%, #和 # #才有区别 only when the * match symbol is used in pattern. Pattern in the structure supports wildcards, * represents 0 or more arbitrary characters,? represents 0 or one arbitrary character, [...] Matches the characters inside the brackets, [!...] Represents a character that does not match the brackets inside
Bogon:/home/bash # var=testcase
Bogon:/home/bash # echo $var
testcase
Bogon:/home/bash # echo ${var%s*e}
TESTCA
Bogon:/home/bash # echo $var
testcase
Bogon:/home/bash # echo ${var%%s*e}
te
bogon:/ Home/bash # echo ${var#?e}
stcase
Bogon:/home/bash # echo ${var##?e}
stcase
Bogon:/home/bash # Echo ${var##*e}

Bogon:/home/bash # echo ${var##*s}
e
Bogon:/home/bash # echo ${var# #test}
case

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.