Braces and parentheses in shell

Source: Internet
Author: User
Tags echo command

The braces and parentheses in shell I want to talk about here are several types of shell parentheses, the structure of braces and the variables in parentheses. The command usage is as follows: www.2cto.com 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. the original form of variables in Shell: $ {var} common variables are $ var, for example, $ var = test $ echo $ vartest, but when you want to display variable values with random characters (I use AA here), an error will occur, as shown below: $ echo $ varAA $ the original form of the variable should be used: $ {var}, that is, a braces should be added to limit the range of the variable name, as shown in the following $ echo $ {var} AAtestAA $, we can easily write a program with a batch extension name. I name it mymv. The program is as follows :#! /Bin/bash tail = $ 1for filename in 'LS' domv $ filename $ {filename }. $ taildone requires a suffix, such as c, to indicate a c program file with a suffix of C. See the test below: $ lsa B c $ mymv c $ lsa. c B. c. c $ looks like the program runs very well, but this is an incomplete program. There are two issues to note: A. There are no subdirectories in the directory. If there is A directory, it is assumed that it is dir, it will also be changed to dir. c. This is obviously not what we want. We should correct this program to recognize directories. B. It does not help to process program parameters. The program should be friendly enough and should be able to process the program when the user does not specify the suffix. For example, the above will directly add a dot (.) to the file (.), this is obviously not what we want. Because our purpose is to explain $ {var}, this is enough, so the above program will not be corrected here. Www.2cto.com 2. command Replace $ (cmd) command Replace $ (cmd) and symbol 'cmd' (note that this is not a single quotation mark. On an American keyboard, 'is the key under ESC) there are similarities $ lsa B c $ echo $ (ls) a B c $ echo 'LS' a B c Let's analyze the command echo $ (ls ), in order to understand what the so-called command replacement means: shell scans the command line once and finds the $ (cmd) structure, it executes cmd in $ (cmd) once to get its standard output, then place the output to the $ (ls) position in the original command echo $ (ls), that is, $ (ls) is replaced, and then the echo command is executed. Echo $ (ls) is replaced with echo a B c. Note that the error output of the command in $ (cmd) is not replaced, but the standard output is replaced: $ var = $ (cat d) ### file d does not exist in the current directory cat: d: there is no such file or directory $ echo $ var $ ### obviously, the value of the var variable is empty 3. A string of commands () and {} are executed on A string of commands, but the difference is: ,() just re-open the sub-shell to execute B for a string of commands, and execute C, (), and {} to the current shell to put a string of commands in brackets, in addition, commands are separated by; D. () The last command does not need a semicolon E. {} The last command must use a semicolon F, there must be a space between the first command of {} and the left brace, and each command in () does not have to have spaces with the brackets H ,() and {}, the redirection of a command in the brackets only affects this command, but the redirection outside the brackets affects all the commands in the brackets. Let's look at several examples: $ var = test $ (Var = notest; echo $ var) ### the variable var value is notest, in the sub-shell, notest $ echo $ var ### the value of the parent shell is still testtest $ {var = notest; echo $ var ;} ### note that there must be a space between the left brace and var: notest $ echo $ var ### change the value of the var variable in the parent shell to notestnotest ${var1 = test1; var2 = test2; echo $ var1> a; echo $ var2 ;### the output test1 is redirected to file, test2 ### while the test2 output is still output to the standard output. $ Cat atest1 ${var1 = test1; var2 = test2; echo $ var1; echo $ var2;}> a ### the standard output of commands in parentheses is all redirected to file a $ cat atest1test2. Below is an example: (echo "1" echo "2 ") | awk '{print NR, $0}' 4, several special replacement structures: $ {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. Var: www.2cto.com $ 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 matches can be found only when Wildcards are used in pattern. Otherwise, there is no longest and shortest matches. The pattern in the structure www.2cto.com supports wildcard characters. * 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.