Parentheses usage in Linux

Source: Internet
Author: User
Tags arithmetic

First, the parentheses in Linux (also called parentheses) "()" Usage 1. Single parenthesis ()

1) Array assignment or initialization of arrays;

2) Child Shell assignment: The contents of the parentheses open a child shell to run independently, the parentheses are concatenated with semicolons, the last command is not required, and the commands and parentheses do not need spaces;

3) Implement multiple command sets: A new command to execute, each command separated by semicolons, the last command must be separated by semicolons;

4) with "$" to implement command substitution: equivalent to cmd scanning the command line, found the $ (CMD) structure, the $ (cmd) in the cmd execution once, get its standard output, and then put this output to the original command. This form should be noted for the shell type used.

2. Double parentheses (())

1) Omit "$" for arithmetic operations, and support the use of "," to split multiple expressions within parentheses:

2) $ (exp) and expr exp effect same, calculate mathematical expression exp value; Calculate logical operations (commonly used for arithmetic operations comparisons, variables in double brackets can be separated by "," to support multiple expressions);

3) Support the step-by-step operation:

Second, the use of brackets in Linux (also called square brackets) "[]" 1. Single brackets []

1) Bash's internal commands, [and test are equivalent. 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.
2) used when comparing operations. The comparison operators available in Test and [] are only = = and! =, both are used for string comparisons and are not available for integer comparisons; 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.


3) 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. For example: [0-9], [A-z] and so on.
4) In the context of an array structure, brackets are used to refer to the number of each element in the array.

2. Double brackets [[]]

1) [[is the key word for the Bash program 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.
2) supports pattern matching of strings, and even supports shell 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.
3) use [[...]] 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.
4) bash considers expressions in double brackets as a single element and returns an exit status code.

Third, the curly braces in Linux (also called curly braces) "{}" Usage 1. The value of the expression variable. In the case of ambiguity, the braces are optional, but it is recommended to add them.

2. Expand with curly braces: You can use braces to bulk-manipulate files

1) First: Expand the comma-delimited list of files in curly braces. such as: Touch {file1,file2,file3}.sh.

2) The second type: in curly braces with dots (.. ) to expand the list of sequential files that are split. such as: Touch {1..10}.sh

3. Special Replacement--${var:-string},${var:+string},${var:=string},${var:?string}

1) ${var:-string}: When the var value of the variable is NULL, ${var:-string} takes the string as a value, and when the variable var value is not NULL, ${var:-string} takes the value of Var as a variable.

2) The substitution rule of ${var:+string}:${var:+string} is the opposite of the above, that is, when the value of variable var is not empty, the value is changed to string, and when Var is empty, it is not replaced or substituted for the variable var value, that is, null value.

3) ${var:=string}: When the variable var value is empty, ${var:=string} assigns it to a string, and the variable VAR is also assigned a string, and if the variable var is not NULL, the value of ${var:=string} is the value of the variable var. This rule is similar to ${var:-string}, except that if the variable is empty, the rule assigns the value after "=" to the variable. (A common use is to determine if a variable is assigned a value, and if not, give it a default value.) )

4) ${var:?string}: If Var is not NULL, replace ${var:?string} with the value of variable Var, and if Var is null, the string is output to standard error and exited from script. You can use this attribute to check whether the value of a variable is set.

Note: The position of a string in the four alternative structures above must not necessarily be a constant, or it can be a variable or an instruction.

4. Pattern match replace--${var%pattern},${var%%pattern},${var#pattern},${var# #pattern} is to remove the left side (on the keyboard # on the left side);% is to remove the right (on the keyboard% on the right of $); The single symbol in is the minimum match, and the two same symbol is the maximum match.

1) The first mode: ${variable%pattern}, this mode, the shell in the variable to find out whether it is to give the pattern of the end, if it is, the variable of the contents of the right to remove the shortest matching mode.

2) The second mode: ${variable%%pattern}, in this mode, the shell looks in the variable to see if it ends with a given pattern pattern, and if so, removes the longest matching pattern on the right side of the variable.

3) Third mode: ${variable#pattern} In this mode, the shell looks in the variable to see if it starts with the pattern patterns given, and if so, removes the shortest matching pattern on the left side of the variable.

4) Fourth mode: ${variable# #pattern} In this mode, the shell looks in the variable to see if it ends with the pattern patterns given, and if so, removes the longest matching pattern on the right side of the variable.

None of these four modes will change the value of variable, where the "" match symbol is used only in pattern, and% and percent, #和 # #才有区别. The pattern in the structure supports wildcard characters,"" means 0 or more arbitrary characters, "?" Indicates that matches only one arbitrary character, [...] Matches the characters inside the brackets, [!...] Indicates a mismatch between the characters in the brackets.

5. String extraction and substitution--${var:num},${var:num1:num2},${var/pattern/pattern},${var//pattern/pattern}

1) First mode: ${var:num},shell extracts all characters from Num to the end in Var. If NUM is positive, start at the left 0, and if NUM is negative, extract the string from the right, but you must use a space after the colon or a number or the entire num with parentheses, such as ${var:-2}, ${var:1-3}, or ${var: (-2)}.


2) The second mode: ${VAR:NUM1:NUM2},NUM1 represents the position, num2 represents the length. The meaning is to extract a substring of length $num2 from the $NUM1 position of the $var string. Both NUM1 and num2 cannot be negative. When a negative number appears, the second ":" and its subsequent contents are automatically ignored, equivalent to executing ${var:num}.

3) The third mode: ${var/pattern/pattern} means replacing the first matching pattern of the Var string with another pattern.

4) The fourth mode: ${var//pattern/pattern} indicates that all matching patterns in the Var string are replaced with another pattern.

6. Multiple command sets:

{cmd1;cmd2;cmd3;} In the current shell Order execution command CMD1,CMD2,CMD3, the commands are separated by semicolons, the last command must have a semicolon, the first command and the opening parenthesis must be separated by a space.

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

Parentheses usage in Linux

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.