Shell size brackets

Source: Internet
Author: User
Tags echo command

There is no doubt about the strength of shell, which makes it easier for us to confuse us. For example, I have always thought that I am smart and cannot tell you clearly.

In Shell, Parentheses, braces, and variables in parentheses are used 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)

Here is a detailed introduction:

1) original form of variables in Shell

The most common form of this variable is $ var, which is used to print the VaR command.

Echo $ VaR

However, there is a problem: an error occurs when you want to display variable values with random characters (such as $ varaa. The system will think that the entire varaa is a variable. In this case, you can use a braces to limit the range of variable names, such as $ {var} AA.

2) Replace the command $ (CMD)

Replace $ (CMD) with the 'cmd' symbol (note that this is not a single quotation mark. On an American keyboard,' is the key under ESC. echo $ (LS) is used to explain the entire replacement process: Shell scans the command line once and discovers the $ (CMD) structure. Then, the CMD in $ (CMD) is executed once, get the standard output, place the output to the $ (LS) position in the original command echo $ (LS), that is, replace $ (LS), and then execute the echo command. As follows:

1

$ Ls

2

A B C

3

$ Echo $ (LS)

4

A B C

5

$ Echo 'LS'

6

A B C

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.

4) Some Special replacement structures: $ {var:-string}, $ {var: + string}, $ {var: = string}, $ {var :? String}

A, $ {var:-string} and $ {var: = string}: If the VaR variable is empty, replace $ {var:-string} with string in the command line }, otherwise, when the variable VAR is not empty, replace $ {var:-string} with the value of the variable VAR. For the replacement rules of $ {var: = string} and $ {var: -string} is the same. The difference is that $ {var: = string} If VaR is empty, use string to replace $ {var: = string, A common use of assigning a string to the variable VAR :$ {var: = string} is to determine whether a variable is assigned a value. If not, assign a default value to the variable.

B. 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)

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.

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.

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.

6) Replace the following four pattern matching structures: $ {var % pattern}, $ {var % pattern}, $ {var # pattern}, $ {var # pattern}

First mode: $ {Variable % pattern}. In this mode, shell searches for the variable mode to check whether it ends with pattern. If yes, remove the content in variable from the command line from the shortest matching mode on the right.

Mode 2: $ {Variable % pattern}. In this mode, shell searches for the pattern in variable to check whether it ends with pattern. If yes, remove the content of variable from the command line from the longest matching mode on the right.

In the third mode: $ {Variable # pattern}, shell searches for the pattern in variable to see if it starts with pattern. If yes, remove the content in variable from the command line from the shortest matching mode on the left.

In the fourth mode: $ {Variable # pattern}, the shell searches for the pattern in variable to see if it ends with pattern. If yes, remove the content of variable from the command line from the longest matching mode on the right.

The value of variable is not changed in the four modes. The difference is between % and %, # And # only when the * matching symbol is used in pattern. 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.

01

[Root @ root shell] # Var = testcase

02

[Root @ root shell] # echo $ VaR

03

Testcase

04

[Root @ root shell] # echo $ {var % S * e} deletes the shortest match from the rightmost side.

05

Testca

06

[Root @ root shell] # echo $ VaR

07

Testcase

08

[Root @ root shell] # echo $ {var % S * e} deletes the longest match from the rightmost side.

09

Te

10

[Root @ root shell] # echo $ var variable has not changed

11

Testcase

12

[Root @ root shell] # echo $ {var #? E} Delete the shortest match from the leftmost

13

Stcase

14

[Root @ root shell] # echo $ VaR

15

Testcase

16

[Root @ root shell] # echo $ {var # * e} Delete the shortest match from the leftmost

17

Stcase

18

[Root @ root shell] # echo $ VaR

19

Testcase

20

[Root @ root shell] # echo $ {var # * e}: Delete the longest match from the leftmost, that is, delete all

21

[Root @ root shell] # echo $ VaR

22

Testcase

23

[Root @ root shell] # echo $ {var # * s} Delete the longest match from the leftmost

24

E

25

[Root @ root shell] # echo $ VaR

26

Testcase

27

[Root @ root shell] # echo $ {var # test} Delete Test

28

Case

29

[Root @ root shell] # echo $ VaR

30

Testcase

31

[Root @ root shell] # echo $ {var # tests} does not match

32

Testcase

Switch from network:

Http://taotaocoder.blog.163.com/blog/static/200228274201110306203483/

Reference: http://blog.chinaunix.net/uid-20104120-id-3286408.html

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.