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 |
if test "a" == "a" |
[]
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 |
if [ "a" == "a" ] |
Let's look at an example of a more complex points:
1 |
a=12 |
[[]]
[[]]
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
, -lt
and other operators. But if [[]]
achieved, we can spend &&
and ||
operators:
1 |
a=12 |
But you will find that we do need to write a mathematical comparison -lt
, -gt
things like that, very sick. Then we can use the following symbols.
let command
During our arithmetic, we can use the let command operations:
1 |
a=10 |
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 |
a=10 |
Or we can calculate the results as an expression, if the result is 0 for false, all the rest is true.
1 |
a=10 |
Or is it:
1 |
a=10 |
$(())
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 |
a=10 |
Output:1
.
1 |
a=10 |
Output:0
.
So if you want to make it as an expression, then it would combine []
symbols. E.g:
1 |
a=10 |
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 |
a = 2 |
The output 2
is: .
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 |
/Users/yurongchan/Yosemite/shell-practice/practice |
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 |
a = 2 |
The above 1
output: .
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.