Shell (), {}

Source: Internet
Author: User

1 execution of a sequence of commands

[Email protected] log]# (A=2;echo $a)

2

[email protected] log]# {A=2;echo $a;} #注意格式的不同, with a space on the left and a semicolon ending at the far right

2


The redirection of a command within 2 () and {} Brackets only affects the command, but redirects outside the brackets affect all the commands in parentheses

[email protected] log]# {A=10;b=100;echo $a >c;echo $b;}

100

[email protected] log]# Cat C

10


[email protected] ~]# {A=10;b=100;echo $a; echo $b;} >c

[email protected] ~]# Cat C

10

100


3 () just re-open a child shell for a sequence of commands to execute, {} Execute on a string of commands in the current shell

[Email protected] ~]# a=100

[Email protected] ~]# (A=10;echo $a)

10

[[email protected] ~]# echo $a #值没有被修改, instructions () The command is implemented in the child shell

100


[Email protected] ~]# a=100

[email protected] ~]# {A=10;echo $a;}

10

[[email protected] ~]# echo $a #值已经被修改, description {} The command is implemented in the current shell

10


4 $ () and ${}

Command substitution:

[[email protected] ~]# echo $ (LS)

Anaconda-ks.cfg a.py a.sh C install.log install.log.syslog moban.sh secure test.txt

The implementation of the variable:

[Email protected] ~]# a=1000

[[email protected] ~]# echo ${a}

1000


5 several special replacement structures

${var:-string},${var:+string},${var:=string},${var:?string}

1) If the variable var is empty or undefined, replace the ${var:-string} with string in the command line, or the variable var is not empty, then replace ${var:-string} with the value of Var.

[Email protected] ~]# echo $var


[[email protected] ~]# echo ${VAR:-BCC}

Bcc

[Email protected] ~]# var=1000

[[email protected] ~]# echo ${VAR:-BCC}

1000


2) The substitution rule for ${var:=string} is the same as the ${var:-string}, and the difference is ${var:=string} if Var is empty, replace ${var:=string} with string, Assign a string to the variable var:

[Email protected] ~]# echo $var


[[email protected] ~]# echo ${VAR:=BBC}

Bbc

[Email protected] ~]# echo $var

Bbc


3)${var:+string}: only if Var is not empty is replaced with a string, if Var is empty then not replace or substitute variable var value, that is, null value


[Email protected] ~]# var=100

[Email protected] ~]# echo $var

100

[[email protected] ~]# echo ${VAR:+BBC}

Bbc

[Email protected] ~]# echo $var

100

[Email protected] ~]# unset var

[Email protected] ~]# echo $var


[[email protected] ~]# echo ${VAR:+BBC}


4)${var:?string}: If Var is not NULL, replace ${var:?string} with the value of variable Var, and if VAR is null, output string to standard error and exit from script. This attribute can be used to check whether the value of a variable is set

[Email protected] ~]# echo $var


[[email protected] ~]# echo ${VAR:?BBC}

-bash:var:bbc

[Email protected] ~]# var=100

[[email protected] ~]# echo ${VAR:?BBC}

100


6 Matching and substitution:

* wildcard character, any number of any characters

# Remove Left

% Remove Right

The single symbol is the minimum match; two symbols are the maximum match


Example:

[Email protected] ~]# var=/var/log/messages

[[email protected] ~]# echo ${var:3:2} #取出从第3个字符开始, 2 characters backwards

r/

[Email protected] ~]# echo ${var:0:3} #取出前3个字符

/va


[Email protected] ~]# var=/var/log/messages

[Email protected] ~]# echo ${var/log/log} #把log替换成LOG

/var/log/messages

[[email protected] ~]# echo ${var//s/$} #两个符号是最大匹配, replace All

/var/log/me$ $age $


[Email protected] ~]# var=/var/log/messages

[[email protected] ~]# echo ${var#*/} #从左边开始, delete the first match */and its left

Var/log/messages characters

[[email protected] ~]# echo ${var#*m}

Essages

[[email protected] ~]# echo ${var##*/} #从左边开始, delete the last match */and its left

Characters of messages

[[email protected] ~]# echo ${var%/*} #从右边开始, delete the first match/* and its right

/var/log characters


This article from "10,000 years too long, seize" blog, please be sure to keep this source http://zengwj1949.blog.51cto.com/10747365/1922976

Shell (), {}

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.