The use of shell parentheses

Source: Internet
Author: User



Parentheses ():

1. The commands in parentheses will be executed in a new sub-shell order, so the variables in parentheses cannot be used by the rest of the script. Multiple commands in parentheses are separated by semicolons, and the last command can have no semicolon, and there is no space between the commands and the parentheses.

2. Get command output--a=$, equivalent to a=$ ' command ', get command output passed to variable a

3. Initialize the array--array= (a B c d), all in the middle with a space delimiter, very similar to an array.


The first one uses:

Error Understanding usage examples :

Note: You can execute multiple commands with parentheses, try () the space between multiple commands, add single quotation marks, and so on, and then execute only the first command.

[email protected] ~]# cat test.sh #!/bin/basharry=$ (ls/usr/local/;ls/tmp) for I in ${arry[@]}do Echo $idone


[Email protected] ~]# ls/usr/local/| Wc-l10[[email protected] ~]# sh test.sh | Wc-l10


---------------------------------------------------------------------------------

Correct usage:

To test the contents of the script:

[[email protected] ~]# cat test.sh #!/bin/basharry=cweb14for i in $ (arry= ' cweb17 '; echo ${arry}) do echo $arry echo $i Done


The process of script execution:

[[email protected] ~]# sh-x test.sh + arry=cweb14++ arry=cweb17# A single child shell is running. + + echo cweb17+ for I in ' $ (arry= ' \ ' Cweb1 7 "\"; Echo ${arry}) ' + Echo cweb14cweb14+ echo cweb17cweb17


Results of the operation:

[Email protected] ~]# sh test.sh cweb14cweb17


The second usage example:

[Email protected] ~]# Ls/usr/local/bin etc games include Lib Lib64 libexec Sbin share src[[email protected] ~]# Cat test.sh #!/bin/basha=6arry=$ (ls/usr/local) #小括号中执行的是shell命令. For i in ${arry[@]}do Echo $idone


Note: The values of the elements in the array are listed with a for loop. The variable arry is already an array after it gets the value.


Operation Result:

[Email protected] ~]# sh test.sh binetcgamesincludeliblib64libexecsbinsharesrc

------------------------------------------------------------------------------

Another use of the second article:


The MySQL command outputs 2 values.

[Email protected] ~]# mysql-e "show slave Status\g" |grep-e "slave_io_running| Slave_sql_running: "|awk ' {print $} ' NoYes


The value of the script loop variable.

[[email protected] ~]# cat test.sh #!/bin/basharry= ($ (mysql-e "show slave Status\g" |grep-e "slave_io_running| Slave_sql_running: "|awk ' {print $} ')) echo $arryfor i in ${arry[@]}do echo $idone


Note: $ () in meaning () executes the command, $ () gets the command value, and then the outside () is the value that initializes the $ ().


[Email protected] ~]# sh test.sh nonoyes


The third use:

[email protected] ~]# cat test.sh #!/bin/basha=6arry= (1 2 3 4 $a) for I in ${arry[@]}doecho $idone


Note: The elements of an array in parentheses can be variables that do not affect the initialization of the array.

Operation Result:

[[Email protected] ~]# sh test.sh 12346




This article is from the "green shirt and clothes" blog, please make sure to keep this source http://215687833.blog.51cto.com/6724358/1980639

The use of shell parentheses

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.