Shell parentheses, brackets, curly braces

Source: Internet
Author: User
Tags arithmetic echo command

1. The prototype of the variables in the shell: ${var} Execution of a sequence of commands
#等价于 $ var=echoEcho
2. Command replacement $ (cmd) execution of a string of commands

command to replace $ (cmd) and sign ' cmd ' (note that this is not a single quote, on an American keyboard, ' is the key below ESC ') has the same

LSEcho $ (lsecho 'ls

Let's analyze the command echo $ (LS) in order to understand what the so-called command substitution means:
The shell scans the command line again, finds the $ (CMD) structure, executes the cmd in $ (cmd) once, obtains its standard output, and then places the output in the original command, the $ (LS) position in echo $ (LS), which is the replacement of the $ (LS) and then the Echo command.

() and {} are executed on a string of commands, but differ:
A, () just re-open a sub-shell for a sequence of commands to execute
b,{} Executes a string of commands in the current shell

3. Extended calculation of the POSIX standard: $ (exp) for mathematical calculations

This calculation is an operator that conforms to the C language, meaning that any operator that conforms to C can be used in $ (exp) or even a trinocular operator.
Note: This extended calculation is an integer-type calculation and does not support floating-point types. If the logical judgment, the expression exp is true is 1, false is 0.

$Echo$((3+2)) 5 $ Echo$((3>2)) 1 $ Echo$(( -<3?2:3)) 3 $ Echo$var $Echo$ ((var=2+3)) 5 $ Echo$var5 $ Echo$ ((var++)) 5 $ Echo$var6 
4. Single brackets []

Bash's internal commands, [and test are equivalent.

The comparison operators available in Test and [] are only = = and! =, both for string comparisons, not for integer comparisons, and for integer comparisons to use only the-eq,-gt form. The greater than sign is not supported either for string comparisons or for integer comparisons.

5. Double brackets [[]]

[[is a key word for the Bash programming language.] use [[...]] Conditional judgment structure. For example,,&&, | |, <, and > operators can normally exist in the [[]] conditional judgment structure, but if they appear in the [] structure, an error will be found.

4, 5 for example:

if($i <5)if[$i-lt 5 ]if[$a-ne 1-a $a! = 2 ]if[$a-ne 1] && [$a! = 2 ]if[[$a! = 1 && $a! = 2 ]]  forIinch$ (SEQ 0 4);d o echo $i;d One forIinch' SEQ 0 4';d o echo $i;d One for((i=0;i<5;i++) );d o echo $i;d One forIinch{0..4};d o echo $i;d one

First, look at the combination of $ with various parentheses in the shell:

$ () Run shell command return output

such as $ (ls-a)

$ (()) arithmetic operation

such as $ (($a + $b))

$[] Arithmetic operations

such as $[$a + $b]

${} variable invocation

such as ${$var}

Take a look at the individual use cases:

() a regular expression can represent a grouping, and is later called by \1,\2, etc.

[] to test an expression

[[]] testing an expression

{} can be used to enclose the entire command block

It is hard to remember, and Hayashi in turn summarizes the methods of variable invocation, command invocation, test expressions, and arithmetic operations:

Variable invocation:

Method one: ${var}

Method Two: $var

Command invocation:

Method one: ' COMMAND '

Method Two: $ (COMMAND)

To test an expression:

Method one: [Expression]

Method two: [[expression]]

Method Three: Test expression

Arithmetic operations

Method One: Let arithmetic operation expression

Let c= $A + $B Here the variables A and B before the $ can be omitted

Method Two: $[arithmetic operation expression]

c=$[$A + $B]

Method Three: $ ((arithmetic operation expression))

c=$ (($A + $B))

Method Four: Expr arithmetic operation expression, the expression in the operands and operators to have a space between, and to use the command reference, encountered multiplication of the * number and escaped

c= ' expr $A + $B '

Method Five: Expr $[arithmetic operation expression], encountered multiplication * number do not escape.

Dd

Shell parentheses, brackets, curly braces

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.