Summary of braces and parentheses in shell

Source: Internet
Author: User
Tags echo d

The braces and parentheses in shell are described in detail. 1. braces and parentheses in shell 1. $ {var} 2. $ (cmd) 3. () and {} 4. $ {var:-string}, $ {var: + string}, $ {var: = string}, $ {var :? String} 5. $ (exp) 6. $ (var % pattern), $ (var % pattern), $ (var # pattern), $ (var # pattern) are described as follows: 1. $ {var} is the original variable in shell. The common form is $ var, for example, $ var = test $ echo $ vartest. However, you cannot add any characters after the variable, for example: $ varAA indicates that an error occurs. You can use the original $ {var} AAtestAA feature to modify the suffix file rename in batches. sh #! /Bin/bashextension = $ 1for filename in 'Find. -type f'domv $ filename $ {filename }. $ extensiondone usage $. /rename. sh c can change all files. c file 2. $ (cmd) is a command replacement such as: $ ls d e f $ echo $ (ls) d e f. This should be understood in this way. First, execute $ (ls ), this standard output replaces the $ (ls) position as the echo parameter, that is, echo d e f. Note: This is consistent, I am used to seeing that only the standard output can be replaced, and the error output cannot be replaced. () and {} are both executed on a string of commands, but there are some differences 1. () Only execute a sub-process re-opening a string of commands {} is executed in the current process 2. () and the commands in {} are separated by; () The last command can be separated by a semicolon; the last semicolon of {} needs 3. () There is no space between the First Command and the brackets. {} There is a space between the First Command and the left bracket for 1: $ var = 10 $ (var = 20; echo $ var) 20 $ echo $ var10 description () the statement executed in does not call the current variable, so $ var = 30 $ {var = 40 in the sub-process; echo $ var} # The first one must contain spaces. 40 $ echo $ var40 indicates that var has been modified and is implemented in the same process. $ {var:-string}, $ {var: + string}, $ {var: = string}, $ {var :? String} A, $ {var:-string} and $ {var: = string} if the var variable is empty, use string in the command line to replace $ {var: -string}. Otherwise, when the variable var is not empty, replace $ {var:-string} with the value of the variable var. For example: $ echo newvar $ echo $ {newvar: -a} a $ echo newvar ### the value of the newvar variable is still null, but $ {newvar: -a} is replaced with a $ newvar = B $ echo $ {newvar:-a }## when the value of the variable newvar is not empty, $ {newvar: -B} is replaced with $ newvar, that is, the replacement rules of bb $ for $ {var: = string} are the same as those of $ {var:-string, the difference is that $ {var: = string}, if var is null, $ {var: = string} is replaced with string, and the string is assigned to the variable va. R: $ echo newvar $ echo $ {newvar: = a} a $ echo newvar ### the variable newvar is assigned as a and $ {newvar: = a} is replaced with aa $ echo $ {newvar: = B }### the variable newvar is not empty (its value has been assigned as a), then $ {newvar: = B} is replaced with the value of newvar (that is, B) a $ echo $ newvara $ {var: = string}. a common usage is to determine whether a variable is assigned a value, if not, assign a default value to it. For example, set the Default editor: PHP code: echo You use EDITOR :$ {editor :=/ bin/vi} B, $ {var: + string }$ {var: + string}'s replacement rule is opposite to the above, that is, it is replaced with string only when var is not empty, if var is null, It is not replaced or replaced with the value of the variable var, that is, the null value. (Because the var variable is empty at this time, the two statements are equivalent.) $ echo $ newvara $ echo $ {newvar: + B} B $ echo $ newvara $ newvar = $ echo $ {newvar: + B} $ C, $ {var :? String} replacement rule: if the variable var is not empty, replace $ {var:? With the value of the variable var :? String}; if the var variable is null, the string is output to the standard error and exits from the script. We can use this feature to check whether the variable value is set. $ Newvar = $ echo $ {newvar :? Newvar value not set} bash: newvar value not set $ newvar = a $ echo $ {newvar :? No value for newvar is set} a $ additional extension: In the above five replace structures, string is not necessarily a constant value. The value of another variable or the output of a command can be used. $ Echo $ {var:-'date'}, December 31, March 6 02:10:39 CST 2005 $ echo $ {var:-$ (date )} july 22, March 6 02:11:46 CST 2005 $ a = test $ echo $ {var:-$ a} test $5. POSIX standard extension calculation: $ (exp) is a C-language operator, that is, any operator that conforms to C can be used in $ (exp), or even a three-object operator. Note: This extension is an integer computation and does not support floating point computation. If the expression exp is true, it is 1, and false is 0. $ Echo $(3 + 2) 5 $ echo $ (3> 2) 1 $ echo $(25 <3? 2: 3) 3 $ echo $ var $ echo $ (var = 2 + 3) 5 $ echo $ var5 $ echo $ (var ++ )) 5 $ echo $ var6 $ is good. The above example is enough, which also indicates that this expansion operation is very powerful. 6. replace the following four modes: $ {var % pattern}, $ {var % pattern}, $ {var # pattern }, $ {var # pattern}: $ {var % pattern} and $ {var % pattern} indicate matching from the rightmost (that is, the end, $ {var # pattern} and $ {var # pattern} match from the leftmost (beginning. $ {Var % pattern} and $ {var # pattern} are the shortest matches, and $ {var % pattern} and $ {var # pattern} are the longest matches. The longest and shortest matching can be achieved only when the wildcard is used in pattern. Otherwise, there is no longest and shortest matching. The pattern in the structure supports wildcards. * Indicates zero or multiple arbitrary characters ,? It indicates zero or any character, and [...] indicates matching the characters in brackets, [!...] It indicates that the characters in the brackets are not matched. $ Var = aabbbccbbdbb $ echo $ {var % B} aabbbccbbdb $ echo $ {var # a} abbbccbbdbb $ echo $ {var #} abbbccbbdbb $ echo $ {var % * B} aabbbccbbdb $ echo $ {var % * B} $ echo $ {var # a *} abbbccbbdbb $ echo $ {var # *} $

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.