Detailed explanation of the use of braces, brackets, and parentheses in the Linux shell + multiple examples

Abstract: Many people, like me, are definitely not very clear about the various uses of shell brackets. Sometimes they don’t know what they mean when they see other scripts. Today, let’s talk about the usage and explanation of the big, middle and small brackets in bash. I often use bash, so I can only use bash to explain, if other shells are different, please don't blame me.

1. The use of braces (also called curly braces) in the shell "{}":

1 、 $ {var}
Insert picture description here

Explanation: When the variable name and the following content are allowed by variable naming, it is not possible to directly use $var at this time. You must use {} to enclose the variable name

2 、 $ {var: -string}
Insert picture description here

Explanation: When the variable var value is empty, var: − a will use a as the value, when the variable var value is not empty, {var:-a} will use a as the value, when the variable var value is not empty,v a r:- a will now a as to the value , when the variation amount V a R & lt value is not as empty time , {var:var -a} will be a value as a variable bar

3 、 $ {var: + string}
Insert picture description here

Explanation: The replacement rule of ${var:+a} is the opposite of the above, that is, when the value of the variable var is not empty, the value is replaced with a, when the variable var is empty, it is not replaced or replaced with the variable var The value of, that is, the null value. (Because the variable var is empty at this time, I think these two statements are the same)

4 、 $ {var: = string}
Insert picture description here

Explanation: When the variable var value is empty, var: = a will assign it to a, and the variable var is also assigned the value a. If the variable var is not empty, {var:=a} will assign it to a, and the variable var is also assigned a value, if the variable var is not empty,v a r:=a will to their assigned value of a , and a variable amount V a R & lt also is assigned the value of a a a , if the variable amount V a R & lt not is empty when , {var:String = value} is the value of the variable var. (A very common usage is to determine whether a variable is assigned a value, and if not, assign a default value to it.)

5 、 $ {var:? String}
Insert picture description here

Explanation: If the variable var is not empty, replace ${var:?string} with the value of the variable var; if the variable var is empty, output the string to the standard error and exit from the script. We can use this feature to check whether the value of the variable is set.

6. Expansion
Insert picture description here

Explanation: Do you think that the position of a can only be constant? The answer is no, it can still be other variable names or a command.

7 、 $ {var% pattern}

Insert picture description here

Explanation: ${variable%pattern}. In this pattern, the shell looks in the variable to see if it ends with the given pattern pattern. If it is, remove the contents of the variable from the command line with the shortest matching pattern on the right

8 、 $ {var %% pattern}

Insert picture description here

Explanation: ${variable%%pattern}. In this pattern, the shell looks in the variable to see if it is the end of the given pattern. If it is, remove the contents of the variable from the command line with the longest matching pattern on the right

9 、 $ {var # pattern}
Insert picture description here

Explanation: In this pattern of ${variable#pattern}, the shell looks in the variable to see if it starts with the given pattern pattern. If it is, remove the content in the variable from the command line with the shortest matching pattern on the left

10 、 $ {var ## pattern}
Insert picture description here

Explanation: In this pattern of ${variable##pattern}, the shell searches in the variable to see if it is the end of the given pattern. If it is, remove the contents of the variable from the command line with the longest matching pattern on the right.

11. {1,2,3} and {1...n}
Detailed explanation of the use of braces, brackets, and parentheses in the Linux shell + multiple examples


2. The usage of square brackets (also called square brackets) "[ ]" in shell:

1. Single brackets []

①The internal command of bash, [and test are equivalent. If we do not specify the absolute path, we usually use the commands that come with bash. The left square bracket in the if/test structure is the command identifier for calling test, and the right square bracket is used to judge the closing condition. This command takes its parameters as a comparison expression or as a file test, and returns an exit status code based on the result of the comparison. The closing bracket is not necessary in the if/test structure, but it is required in the new version of Bash.

②The only comparison operators available in Test and [] are == and !=, both of which are used for string comparison and cannot be used for integer comparison. Integer comparison can only use the form -eq, -gt. Both string comparison and integer comparison do not support greater than and less than signs. If you really want to use it, you can use the escaped form for string comparison. If you compare "ab" and "bc": [ab <bc ], the result is true, that is, the return status is 0. The logical AND and logical OR in [] are represented by -a and -o.

③Character range. Used as part of a regular expression to describe a range of matched characters. Regulars cannot be used in brackets for test purposes.

④ In the context of an array structure, square brackets are used to refer to the number of each element in the array.

2. Double square brackets [[ ]]

①[[ is a keyword of the bash programming language. Not a command, the [[ ]] structure is more general than the [] structure. All characters between [[ and]] will not have file name expansion or word splitting, but parameter expansion and command substitution will occur.

②Support string pattern matching, even support shell regular expressions when using =~ operator. When comparing strings, you can use the one on the right as a pattern, not just a string, such as [[ hello == hell? ]], the result is true. [[ ]] matches strings or wildcards without quotation marks.

③Using [[… ]] conditional judgment structure instead of […] can prevent many logic errors in the script. For example, the &&, ||, <and> operators can normally exist in the [[ ]] conditional judgment structure, but if they appear in the [] structure, an error will be reported.

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


3. The usage of parentheses "( )" in the shell:

1. () Usage

①, () array assignment
Insert picture description here

②, () subshell assignment

Insert picture description here

The variable var value in the subshell is lookback, but it is not this value in the upper shell. It can be seen that it is a valid assignment in the subshell

③, () command collection result redirection

Insert picture description here

2. The usage of $() I
won’t say much about it, I should know it, it is used to execute a command

3. (()) usage
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_52425873/article/details/113052611