[Posts] bash, (), {}, (()), [], [[]] in distinction bash (), {}, (()), [], [[]] difference

In distinction bash (), {}, (()), [], [[]] of

 
HTTPS: // www.cnblogs.com/marility/p/7259821.html 

least bit of difference hack away 

needs carefully

 

Introduction: bash encountered in the various brackets, while when judging character numeric comparison is performed, constantly a problem, then, at the same time be tested on centos version 6.7 by referring to "advanced bash-scripting guide", the current situation is summarized as follows . If flawed, look correct.

 

 

One.()

A combination of the command, a command group corresponding to

[root@mghuee~chunlanyy testdir]# I=123;(I=xyz;echo $I;);echo $I
xyz
123

 

 

 

two.{}

With "{}", but also for a combination of the command, the difference between "()" is that the former is currently the shell, while the latter are inside the shell

Copy the code
[root@mghuee~chunlanyy testdir]# I=123;(echo $I;I=xyz;echo $I;);echo $I
123
xyz
123
[root@mghuee~chunlanyy testdir]# I=123;{ echo $I;I=xyz;echo $I; };echo $I
123
xyz
xyz
Copy the code

 

Can be seen from the example, "()" is assigned only affect its own sub-shell, and the shell will not be assigned to the parent, and "{}" is only performed in the same shell

Also note that, "{}" is a keyword, so that in the "{" space isolation required, and on both sides simultaneously using the command ";" indicates the end of the command.

[root@mghuee~chunlanyy testdir]# type {
{ is a shell keyword

 

 

 

 

three.[]

"[]" It is mainly used as a determination condition, the determination target comprises comparing the file type and assignment

[root@mghuee~chunlanyy testdir]# type [
[ is a shell builtin

 

We can see the "[" is an internal command, can be considered equivalent to the basic test command

[root@mghuee~chunlanyy testdir]# [ 4 -lt 3 ] && echo yes || echo no
no
[root@mghuee~chunlanyy testdir]#  test 4 -lt 3  && echo yes || echo no
no

 

 

Common comparison test as follows:

Digital test: -eq -ne -lt -le -gt -ge

File test: -r, -l, -w, -x, -f, -d, -s, -nt, -ot

Test String: =, =, - n, -z, \>, \ <!

Logic test: -a, -o ,!

Math: support for integer arithmetic, but has been "((...))" instead of

 

Points to note:

1. Only such a digital comparison -LT-character comparison, but you can not use the "<" Such a comparison operator, even if added escapes "\ <"

[root@mghuee~chunlanyy testdir]# [ 1 > 2 ] && echo yes || echo no
yes

 

Obviously, "1> 2" should be wrong, but show "yes", in fact, here ">" is not greater than the number of arithmetic comparison, and to redirect output to view the current directory by ls command more a file named file 2

[root@mghuee~chunlanyy testdir]# ll -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 20:53 2

 

Imagine someone will escape numerical comparison

[root@mghuee~chunlanyy testdir]# [ 1 \> 2 ] && echo yes || echo no
no

 

Size numeric comparison by comparing this line with our expectations, but "\>" not here to be understood in our daily

[root@mghuee~chunlanyy testdir]# [ a \> 2 ] && echo yes || echo no
yes

 

Here is the ASCII value comparison size comparison, at the same time here can only support single-character size comparison

[root@mghuee~chunlanyy testdir]# [ 5 \> 20 ] && echo yes || echo no
yes

 

 

2. "[]" Is a logical operator support -a, -o ,! Where -a, -o parameter test is equivalent to a command, you can not use &&, ||

[root@mghuee~chunlanyy testdir]# [ 1 -lt  2 && 1 -lt 3 ] && echo yes || echo no
-bash: [: missing `]'
no

 

&& and || strong logical operation, will split command, has been split into the above example, "[1 -lt 2" && "1 -lt 3]", it will show no pairing "["

 

In the logical operations, [expre1 -a expre 2] and [[expre1 && expre2]] is not entirely consistent, logical && will short-circuit operation, but not -a

[root@mghuee~chunlanyy testdir]# [ 1 -gt 3 -a 2 > test1.txt ] && echo yes || echo no
no
[root@mghuee~chunlanyy testdir]# ls
test1.txt

 

No short-circuiting operation logic performs all determination, even if "1 -gt 3" has an error, the subsequent operation is still performed, although the result is no, but also generates file test1.txt
[root@mghuee~chunlanyy testdir]# [[ 1 -gt 3 && 2 > test2.txt ]] && echo yes || echo no
no
[root@mghuee~chunlanyy testdir]# ls
test1.txt

 

Logical arithmetic operation, and the command, "1 -gt 3" is false i.e. without subsequent judgment, it appears no test2.txt

 

3. "[]" Is a command, it is a reference variable or constant therein, need to use double quotes or single quotes, because there will be split word (Word-Splitting) Phenomenon

Copy the code
[root@mghuee~chunlanyy testdir]# I=" 29";[ -f $I ] && echo yes || echo no
no
[root@mghuee~chunlanyy testdir]# I=" 29";[ -f "$I" ] && echo yes || echo no
yes
[root@mghuee~chunlanyy testdir]# ll -a
total 8
drwxr-xr-x. 2 root root 4096 Jun 17 23:20 .
drwxrwxrwt. 6 root root 4096 Jun 17 20:53 ..
-rw-r--r--. 1 root root    0 Jun 17 23:17  29
Copy the code

 

There is a file in the current directory named "29" file (29 in front of a space), in the [] is not added when the reference to the variable double quotation marks, [] -f internal execution is determined, "29", because the current directory file does not exist file name "29", showing result is no

 

four.(())

It supports four operations, equivalent to let function

[root@mghuee~chunlanyy testdir]# let I=2+4;echo $I
6
[root@mghuee~chunlanyy testdir]# I=$((2+4));echo $I
6

 

Fives.[[]]

"[[" Is a keyword, for comparison and determination, Basic Support "[]" is determined in all of the comparison operators.

[root@mghuee~chunlanyy testdir]# type [[
[[ is a shell keyword

 

 

"[[" And "[" differences:

1. The logical operator inconsistency, "[[]]" is the "&&", "||", "[]" to "-a", "- o". "[[]]" Shorted support logic, and "[]" is not supported

2. "[[]]" Support for regular expression matching.

3. "[[]]" As a keyword, the same bracket with intermediate spaces must be isolated expression

4. "[[]]" Can not escape when used in comparison operators, while not appear Word-Splitting

Copy the code
[root@mghuee~chunlanyy testdir]# I=" 29";[[ -f $I ]] && echo yes || echo no
yes
[root@mghuee~chunlanyy testdir]# ll -a
total 8
drwxr-xr-x. 2 root root 4096 Jun 17 23:20 .
drwxrwxrwt. 6 root root 4096 Jun 17 20:53 ..
-rw-r--r--. 1 root root    0 Jun 17 23:17  29
Copy the code

 

Compared with the result of the "[]", it is clear, in the "[[]]" and does not appear in the Word-Splitting

[root@mghuee~chunlanyy testdir]# [[ a > 1 ]] && echo yes || echo no
yes
[root@mghuee~chunlanyy testdir]# [[ a \> 1 ]] && echo yes || echo no
-bash: conditional binary operator expected
-bash: syntax error near `\>'

 

As can be seen, error after using the escape

5. [[]] In the "==" using "= -" in

Reference "advanced bash-scripting guide" interpretation of both

== String comparison operator <== string comparison

= ~ Regular Expression match operator <== regular expression matching

 

But this is not representative of the regular expression pattern matching "==" in the "[[]]" can not be achieved, part of the match can be achieved

[root@mghuee~chunlanyy tmp]# [[ abc == a*b***c ]] && echo yes || echo no
yes

 

But only to achieve a match between the characters

[root@mghuee~chunlanyy tmp]# [[ `cat /etc/passwd` == ^root ]] && echo yes || echo no
no
[root@mghuee~chunlanyy tmp]# [[ `cat /etc/passwd` =~ ^root ]] && echo yes || echo no
yes

 

The "= ~" is able to complete by the regular expression to match

 

Note: In the regular expression pattern matching, pattern matching the right not to use double quotes , after the bash 3.2 version has been clearly stated when using regular expressions to match patterns can not be combined with double quotes

Copy the code
[root@mghuee~chunlanyy tmp]# [[ `cat /tmp/123` =~ "^root" ]] && echo yes || echo no
yes
[root@mghuee~chunlanyy tmp]# cat 123
"^root"
[root@mghuee~chunlanyy tmp]# [[ `cat /tmp/123` =~ ^root ]] && echo yes || echo no
no
[root@mghuee~chunlanyy tmp]# cat 123
"^root"
Copy the code

 

Because "[[]]" does not appear Word-Splitting, after joining but let the match pattern becomes "" ^ root ""

 

 

summary:

  1. Use "let command" when an arithmetic operation, or "$ (())" "
  2. Files exist or whether the character is empty "[]"
  3. Logical operator "&&", "||" to be used in the "[[]]" in, "- a", "- o" in "[]" in
  4. In "[[]]" regular expression matching pattern not matched when using double quotes
  5. Numerical size in comparison "[]" in comparison to the use of "-lt", "- gt" Such a comparison operator, can use the "<", ">"
  6. "[[]]" And "[]" are the best holding space on both sides, "[[]]" is a space on both sides must be isolated
  7. The above are customary use, it is not explained in combination with other modes must not use, but can avoid some puzzling errors

Introduction: bash encountered in the various brackets, while when judging character numeric comparison is performed, constantly a problem, then, at the same time be tested on centos version 6.7 by referring to "advanced bash-scripting guide", the current situation is summarized as follows . If flawed, look correct.

 

 

One.()

A combination of the command, a command group corresponding to

[root@mghuee~chunlanyy testdir]# I=123;(I=xyz;echo $I;);echo $I
xyz
123

 

 

 

two.{}

With "{}", but also for a combination of the command, the difference between "()" is that the former is currently the shell, while the latter are inside the shell

Copy the code
[root@mghuee~chunlanyy testdir]# I=123;(echo $I;I=xyz;echo $I;);echo $I
123
xyz
123
[root@mghuee~chunlanyy testdir]# I=123;{ echo $I;I=xyz;echo $I; };echo $I
123
xyz
xyz
Copy the code

 

Can be seen from the example, "()" is assigned only affect its own sub-shell, and the shell will not be assigned to the parent, and "{}" is only performed in the same shell

Also note that, "{}" is a keyword, so that in the "{" space isolation required, and on both sides simultaneously using the command ";" indicates the end of the command.

[root@mghuee~chunlanyy testdir]# type {
{ is a shell keyword

 

 

 

 

three.[]

"[]" It is mainly used as a determination condition, the determination target comprises comparing the file type and assignment

[root@mghuee~chunlanyy testdir]# type [
[ is a shell builtin

 

We can see the "[" is an internal command, can be considered equivalent to the basic test command

[root@mghuee~chunlanyy testdir]# [ 4 -lt 3 ] && echo yes || echo no
no
[root@mghuee~chunlanyy testdir]#  test 4 -lt 3  && echo yes || echo no
no

 

 

Common comparison test as follows:

Digital test: -eq -ne -lt -le -gt -ge

File test: -r, -l, -w, -x, -f, -d, -s, -nt, -ot

Test String: =, =, - n, -z, \>, \ <!

Logic test: -a, -o ,!

Math: support for integer arithmetic, but has been "((...))" instead of

 

Points to note:

1. Only such a digital comparison -LT-character comparison, but you can not use the "<" Such a comparison operator, even if added escapes "\ <"

[root@mghuee~chunlanyy testdir]# [ 1 > 2 ] && echo yes || echo no
yes

 

Obviously, "1> 2" should be wrong, but show "yes", in fact, here ">" is not greater than the number of arithmetic comparison, and to redirect output to view the current directory by ls command more a file named file 2

[root@mghuee~chunlanyy testdir]# ll -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 20:53 2

 

Imagine someone will escape numerical comparison

[root@mghuee~chunlanyy testdir]# [ 1 \> 2 ] && echo yes || echo no
no

 

Size numeric comparison by comparing this line with our expectations, but "\>" not here to be understood in our daily

[root@mghuee~chunlanyy testdir]# [ a \> 2 ] && echo yes || echo no
yes

 

Here is the ASCII value comparison size comparison, at the same time here can only support single-character size comparison

[root@mghuee~chunlanyy testdir]# [ 5 \> 20 ] && echo yes || echo no
yes

 

 

2. "[]" Is a logical operator support -a, -o ,! Where -a, -o parameter test is equivalent to a command, you can not use &&, ||

[root@mghuee~chunlanyy testdir]# [ 1 -lt  2 && 1 -lt 3 ] && echo yes || echo no
-bash: [: missing `]'
no

 

&& and || strong logical operation, will split command, has been split into the above example, "[1 -lt 2" && "1 -lt 3]", it will show no pairing "["

 

In the logical operations, [expre1 -a expre 2] and [[expre1 && expre2]] is not entirely consistent, logical && will short-circuit operation, but not -a

[root@mghuee~chunlanyy testdir]# [ 1 -gt 3 -a 2 > test1.txt ] && echo yes || echo no
no
[root@mghuee~chunlanyy testdir]# ls
test1.txt

 

No short-circuiting operation logic performs all determination, even if "1 -gt 3" has an error, the subsequent operation is still performed, although the result is no, but also generates file test1.txt
[root@mghuee~chunlanyy testdir]# [[ 1 -gt 3 && 2 > test2.txt ]] && echo yes || echo no
no
[root@mghuee~chunlanyy testdir]# ls
test1.txt

 

Logical arithmetic operation, and the command, "1 -gt 3" is false i.e. without subsequent judgment, it appears no test2.txt

 

3. "[]" Is a command, it is a reference variable or constant therein, need to use double quotes or single quotes, because there will be split word (Word-Splitting) Phenomenon

Copy the code
[root@mghuee~chunlanyy testdir]# I=" 29";[ -f $I ] && echo yes || echo no
no
[root@mghuee~chunlanyy testdir]# I=" 29";[ -f "$I" ] && echo yes || echo no
yes
[root@mghuee~chunlanyy testdir]# ll -a
total 8
drwxr-xr-x. 2 root root 4096 Jun 17 23:20 .
drwxrwxrwt. 6 root root 4096 Jun 17 20:53 ..
-rw-r--r--. 1 root root    0 Jun 17 23:17  29
Copy the code

 

There is a file in the current directory named "29" file (29 in front of a space), in the [] is not added when the reference to the variable double quotation marks, [] -f internal execution is determined, "29", because the current directory file does not exist file name "29", showing result is no

 

four.(())

It supports four operations, equivalent to let function

[root@mghuee~chunlanyy testdir]# let I=2+4;echo $I
6
[root@mghuee~chunlanyy testdir]# I=$((2+4));echo $I
6

 

Fives.[[]]

"[[" Is a keyword, for comparison and determination, Basic Support "[]" is determined in all of the comparison operators.

[root@mghuee~chunlanyy testdir]# type [[
[[ is a shell keyword

 

 

"[[" And "[" differences:

1. The logical operator inconsistency, "[[]]" is the "&&", "||", "[]" to "-a", "- o". "[[]]" Shorted support logic, and "[]" is not supported

2. "[[]]" Support for regular expression matching.

3. "[[]]" As a keyword, the same bracket with intermediate spaces must be isolated expression

4. "[[]]" Can not escape when used in comparison operators, while not appear Word-Splitting

Copy the code
[root@mghuee~chunlanyy testdir]# I=" 29";[[ -f $I ]] && echo yes || echo no
yes
[root@mghuee~chunlanyy testdir]# ll -a
total 8
drwxr-xr-x. 2 root root 4096 Jun 17 23:20 .
drwxrwxrwt. 6 root root 4096 Jun 17 20:53 ..
-rw-r--r--. 1 root root    0 Jun 17 23:17  29
Copy the code

 

Compared with the result of the "[]", it is clear, in the "[[]]" and does not appear in the Word-Splitting

[root@mghuee~chunlanyy testdir]# [[ a > 1 ]] && echo yes || echo no
yes
[root@mghuee~chunlanyy testdir]# [[ a \> 1 ]] && echo yes || echo no
-bash: conditional binary operator expected
-bash: syntax error near `\>'

 

As can be seen, error after using the escape

5. [[]] In the "==" using "= -" in

Reference "advanced bash-scripting guide" interpretation of both

== String comparison operator <== string comparison

= ~ Regular Expression match operator <== regular expression matching

 

But this is not representative of the regular expression pattern matching "==" in the "[[]]" can not be achieved, part of the match can be achieved

[root@mghuee~chunlanyy tmp]# [[ abc == a*b***c ]] && echo yes || echo no
yes

 

But only to achieve a match between the characters

[root@mghuee~chunlanyy tmp]# [[ `cat /etc/passwd` == ^root ]] && echo yes || echo no
no
[root@mghuee~chunlanyy tmp]# [[ `cat /etc/passwd` =~ ^root ]] && echo yes || echo no
yes

 

The "= ~" is able to complete by the regular expression to match

 

Note: In the regular expression pattern matching, pattern matching the right not to use double quotes , after the bash 3.2 version has been clearly stated when using regular expressions to match patterns can not be combined with double quotes

Copy the code
[root@mghuee~chunlanyy tmp]# [[ `cat /tmp/123` =~ "^root" ]] && echo yes || echo no
yes
[root@mghuee~chunlanyy tmp]# cat 123
"^root"
[root@mghuee~chunlanyy tmp]# [[ `cat /tmp/123` =~ ^root ]] && echo yes || echo no
no
[root@mghuee~chunlanyy tmp]# cat 123
"^root"
Copy the code

 

Because "[[]]" does not appear Word-Splitting, after joining but let the match pattern becomes "" ^ root ""

 

 

summary:

  1. Use "let command" when an arithmetic operation, or "$ (())" "
  2. Files exist or whether the character is empty "[]"
  3. Logical operator "&&", "||" to be used in the "[[]]" in, "- a", "- o" in "[]" in
  4. In "[[]]" regular expression matching pattern not matched when using double quotes
  5. Numerical size in comparison "[]" in comparison to the use of "-lt", "- gt" Such a comparison operator, can use the "<", ">"
  6. "[[]]" And "[]" are the best holding space on both sides, "[[]]" is a space on both sides must be isolated
  7. The above are customary use, it is not explained in combination with other modes must not use, but can avoid some puzzling errors

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12166118.html