Shell Quick Start (XVIII): the use of special symbols

In the Shell language, often see special identification brackets and parentheses, for example: [], [[]], (()), $(()), (). These symbols are often so we are very confused, to clarify the role and the difference between them is necessary.

Before we begin, let's learn a test command.

test command

The test command is mainly used for determining the expression of its grammatical structure is as follows:

1
test {EXPRESSION}

E.g:

1
2
3
4
if test "a" == "a"
then
echo match!
fi

[]

In fact, [] symbol acting as the test command, all expressions for judging the truth. Only [] enclosing expressions of it, more readable. The example above with [] override this becomes:

1
2
3
4
if [ "a" == "a" ]
then
echo match!
fi

Let's look at an example of a more complex points:

1
2
3
4
5
a=12
if [ $a -gt 10 -a $a -lt 15 ]
then
echo match!
fi

[[]]

[[]]Symbol and []the difference symbol that in [[]]the symbol, the reference variables when we can no longer use the $ symbol, but also use && and || operators.

Like the above range, a determination of the variable, in our []notation, only -gt, -a, -ltand other operators. But if [[]]achieved, we can spend &&and ||operators:

1
2
3
4
5
a=12
if [[ a -gt 10 && a -lt 15 ]]
then
echo match!
fi

But you will find that we do need to write a mathematical comparison -lt, -gtthings like that, very sick. Then we can use the following symbols.

let command

During our arithmetic, we can use the let command operations:

1
2
3
a=10
let b=a+10
echo $b

In the let command variables you do not need to use the $ symbol can be used. Like above a variable, in fact, a variable, but do not need to use the $ symbol let statement on line 2 can be successful operation.

(())

This action symbol group and let command is similar, used in the arithmetic operation, is built bash function. So, in the implementation efficiency is better than let a lot of command.

In this notation, we can integer operations, its role and let command.

1
2
3
a=10
(( b = a + 10 ))
echo $b

Or we can calculate the results as an expression, if the result is 0 for false, all the rest is true.

1 
2
3 large column  Shell Quick Start (XVIII): the use of special symbols
4
5
a=10
if (( a + 10 ))
then
echo true
fi

Or is it:

1
2
3
4
5
a=10
if (( a <= 12 && a > 0))
then
echo great than 10
fi

$(())

And on top of this stuff almost, but not the same as the command returns a value, but will replace it like a variable result of the operation. E.g:

1
2
3
a=10
b=$(( a <= 12 && a > 0))
echo $b

Output:1 .

1
2
3
a=10
b=$(( a <= 12 && a < 0))
echo $b

Output:0 .

So if you want to make it as an expression, then it would combine []symbols. E.g:

1
2
3
4
5
a=10
if [ $(( a <= 12 && a > 0)) -eq 1 ]
then
echo great than 10
fi

For (())symbolic purposes only bash Shell has this, but $(())it is all Shell has, more versatile.

()

() Shell notation brackets run, the results do not interfere with the outer Shell as a child.

Consider the following example:

1
2
3
a = 2 
(a = 1)
Echo $ a

The output 2is: .

Because a child is in brackets Shell, Shell not affect the operation of the outer layer, so a value of 1 does not affect the result of the outer layer, the outer layer 2, or a variable.

Using this feature on the face Shell, we write Shell scripts can be done without switching the current directory and do some thing in a different directory. E.g:

1
(cd hello; echo "Hello Shell" > hello.txt); pwd; cat hello/hello.txt

I entered above the subdirectory hello, and created a hello.txt document. The output is:

1
2
/Users/yurongchan/Yosemite/shell-practice/practice
Hello Shell

I can see no change in the current directory, but the document has been created successfully.

Braces {} (Block of code)

This use of the above described command groups are very similar, but there is a difference, it performs in the current shell, no subshell. When only simple to use braces, acts like a function does not specify the name of the general.

1
2
3
a = 2 
{a = 1; }
Throw $ a

The above 1output: .

This usage and ()the difference between the two uses:

  • Braces {}in Shell operations in the current run, the result will affect the outer layer, and the brackets ()are not.
  • Braces final statement must use a ;semicolon, otherwise error, and brackets ()and do not have this requirement.

Guess you like

Origin www.cnblogs.com/lijianming180/p/12014314.html
Recommended