Parentheses, braces, and variables in shell. command usage

Source: Internet
Author: User
Tags echo command

Here I want to talk about the parentheses, braces, and variables in several shells. The command usage is as follows:

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)

The statement is as follows:
1. original form of variables in shell: $ {var}
Common variables are $ var, such

$ Var = test
$ Echo $ VaR
Test

However, when you want to display variable values with random characters (I use AA here), an error will occur, as shown below:

$ Echo $ varaa

$

In this case, the original form of the variable $ {var} should be used, that is, a braces should be added to limit the range of the variable name, as shown below:
$ Echo $ {var} AA
Testaa
$

With this feature, we can easily write a program with the suffix modified in batches. I name it mymv. The program is as follows:
#! /Bin/bash

Tail = $1
For filename in 'LS'
Do
MV $ filename $ {filename}. $ tail
Done

The program needs to provide a suffix, such as C, to indicate the C program file with a suffix of C, see the following test:
$ Ls
A B C
$ Mymv C
$ Ls
A. c B. C c. c. c
$

It seems that the program runs 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, if it is Dir, it will be changed to Dir. C. This is obviously not what we want. We should correct this program to recognize the directory.
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.

2. Replace $ (CMD) with the command)
Replace $ (CMD) with the 'cmd' symbol (note that this is not a single quotation mark. On an American keyboard,' is the key under ESC ).
$ Ls
A B C
$ Echo $ (LS)
A B C
$ Echo 'LS'
A B C

Let's analyze the command echo $ (LS) to understand what the so-called command replacement means:
Shell scans the command line once and finds the $ (CMD) structure. Then, 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.
As follows:
Echo $ (LS) is replaced with ECHO A B C
Note that the error output of the command in $ (CMD) will not be replaced, but only the standard output will be replaced:
$ Var = $ (Cat D) ### the file D does not exist in the current directory
Cat: D: No file or directory
$ Echo $ VaR

$ ### Obviously, the value of the VaR variable is null.

3. A string of command execution () and {}
() And {} both execute a string of commands, but there are some differences:
A, () just re-open a sub-shell for executing a string of commands
B, {} executes a string of commands in the Current Shell
C, () and {} both place a string of commands in brackets and separate them with a comma (;).
D, () The last command does not need a semicolon.
E, {} The Last Command should use a semicolon
There must be a space between the first command of F, {} and the left parenthesis.
The commands in G, () do not have to have spaces with parentheses
The redirection of a command in the brackets h, () and {} 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, which is valid in the sub-shell.
Notest
$ Echo $ var ### the value of the parent shell is still test
Test
$ {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 notest
Notest
${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
Test1
${Var1 = test1; var2 = Test2; echo $ var1; echo $ var2;}> A ### the standard output of the commands in parentheses is redirected to file.
$ Cat
Test1
Test2

The following 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, if the VaR variable is not empty, replace $ {var:-string} with the VaR value}
For example:
$ Echo newvar

$ Echo $ {newvar:-}
A
$ Echo newvar ### the value of the newvar variable is still null, but $ {newvar:-A} in the previous command line is replaced with

$ Newvar = B
$ Echo $ {newvar:-A }### when the value of the newvar variable is not empty, $ {newvar:-B} In this command line is replaced with $ newvar, that is, B
B
$

The replacement rules for $ {var: = string} are the same as those for $ {var:-string}. The difference is that if VaR is null, when $ {var: = string} is replaced with a string, the string is assigned to the variable VAR:

$ Echo newvar

$ Echo $ {newvar: =}
A
$ Echo newvar ### the variable newvar is assigned as a, and $ {newvar: = A} is replaced with
A
$ Echo $ {newvar: = B }### the newvar variable 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 $ newvar
A

$ {Var: = string} is often used to determine whether a variable is assigned a value. If not, assign a default value to the variable.
For example, set the Default Editor:
PHP code:
Echo you use editor :$ {editor :=/ bin/VI}

B, $ {var: + string}
The replacement rule of $ {var: + string} is opposite to the above rule, 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 variable VAR is empty at this time, the two statements are equivalent)
$ Echo $ newvar
A
$ Echo $ {newvar: + B}
B
$ Echo $ newvar
A
$ Newvar =
$ Echo $ {newvar: + B}

$

C, $ {var :? String}
The replacement rule is: 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: the value of newvar is not set.
$ Newvar =
$ Echo $ {newvar :? Newvar value not set}
A
$

Supplementary Extension: In the above five replace structures, string is not necessarily a constant value. It can be the value of another variable or the output of a command.
$ Echo $ {var:-'date '}
January 1, March 6 02:10:39 CST 2005
$ Echo $ {var:-$ (date )}
January 1, March 6 02:11:46 CST 2005
$ A = test
$ Echo $ {var:-$}
Test
$

5. POSIX standard extended computing: $ (exp ))
This computation is an operator that complies with the C language, that is, any operator that complies with 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 $ VaR
5
$ Echo $ (VAR ++ ))
5
$ Echo $ VaR
6
$

Well, the above example is enough, which also shows that this expansion operation is very powerful.

6. Replace the following four modes: $ {var % pattern}, $ {var % pattern}, $ {var # pattern}, $ {var # pattern}
The meaning of these four structures is: $ {var % pattern} and $ {var % pattern} indicate matching from the rightmost (that is, the end), $ {var # pattern}
And $ {var # pattern} match from the leftmost (that is, the beginning. $ {Var % pattern} and $ {var # pattern} are the shortest horses.
Configuration, $ {var % pattern} and $ {var # pattern} are the longest matches. The longest and shortest match can be found only when Wildcards are used in pattern. Otherwise, no
Long 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 % B}
Aabbbccbbdb
$ Echo $ {var #}
Abbbccbbdbb
$ Echo $ {var #}
Abbbccbbdbb
$ Echo $ {var % * B}
Aabbbccbbdb
$ Echo $ {var % * B}

$ Echo $ {var # *}
Abbbccbbdbb
$ Echo $ {var # *}

$

The above is a simple example of four modes to replace the structure.

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.