Linux shell programming study notes 23: Summary of usage of [] [[]]

Last time I sorted out the usage of (), $() and (()) in Linux Shell programming, now I will sort out the usage of [] and [[]].

1 Single bracket (square bracket) []


1.1 Check whether a certain condition is true


[Identical to test, it is a built-in Shell command used to test whether a certain condition is true. The exit status is 0 when the condition is true, otherwise it is a non-zero value.

1.1.1 String comparison operations

1.1.1.1 == (or =) and !=

In Test and [], the only available comparison operators are == (or =) and !=, both of which are used for string comparison and cannot be used for integer comparison.

  • Variable names in [] do not need to be prefixed with $ as long as there is no ambiguity.

Example:

We first define the strings s1="abc" and s2="def"

Then use == and != in [] respectively to test in bash and zsh.

1. In bash

[csdn ~]$ s1="abc"; s2="def"
[csdn ~]$ echo $[ $s1 != $s2 ] 
0
[csdn ~]$ if [ $s1 != $s2 ] ; then echo s1=$s1; else echo s2=$s2; fi 
s1=abc
[csdn ~]$ echo $[ $s1 == $s2 ] 
1
[csdn ~]$ if [ $s1 == $s2 ] ; then echo s1=$s1; else echo s2=$s2; fi 
s2=def
[csdn ~]$ echo $[ s1 == s2 ] 
1
[csdn ~]$ 

2. In zsh

csdn @ edu $ s1="abc"
csdn @ edu $ s1="abc";s2="def"
csdn @ edu $ echo $[ $s1 != $s2 ]
0
csdn @ edu $ if [ $s1 != $s2 ] ; then echo s1=$s1; else echo s2=$s2; fi 
else> 
csdn @ edu $ echo $[ $s1 == $s2 ]
1
csdn @ edu $ if [ $s1 == $s2 ] ; then echo s1=$s1; else echo s2=$s2; fi 
else>       
csdn @ edu $ echo $[ s1 == s2 ] 

csdn @ edu $ 

1.1.1.2 < and >

For string comparison, can we directly use the greater than sign (>) and less than sign (<)?

1. In bash

[csdn ~]$ s1="abc"
[csdn ~]$ s2="def"
[csdn ~]$ echo $[ $s1 > $s2 ]
0
[csdn ~]$ [ $s1 \> $s2 ]
[csdn ~]$ echo $?
1
[csdn ~]$ echo $[ $s1 < $s2 ]
0
[csdn ~]$ [ $s1 \< $s2 ]
[csdn ~]$ echo $?
0
[csdn ~]$ 

In the above example, we first define the string variables s1="abc" and s2="def", and then directly use > and < in [] to compare s1 and s2, and the displayed results are both 1. There is obviously a problem.

When we use \> and \< in [] to compare s1 and s2, the results are 1 and 0 respectively, which is the correct result.

 It can be seen that in bash, for string comparison, you can use the escape form with the greater than sign (>) and less than sign (<).

2. In zsh

user # csdn zsh $ s1="abc"           
user # csdn zsh $ s2="def"           
user # csdn zsh $ echo $[ $s1 > $s2 ]
0
user # csdn zsh $ [ $s1 \> $s2 ]    
zsh: condition expected: >
user # csdn zsh $ echo $?
1
user # csdn zsh $ echo $[ $s1 < $s2 ]
0
user # csdn zsh $ [ $s1 \< $s2 ]     
zsh: condition expected: <
user # csdn zsh $ echo $?
1

In the above example, we also first defined the string variables s1="abc" and s2="def", and then directly used > and < in [] to compare s1 and s2, and the displayed results are both It's 0, which is different from bash, but still obviously problematic.

When we use \> and \< in [] to compare s1 and s2, zsh does not support it.

Character range. Used as part of a regular expression to describe a matching range of characters. Regular expressions cannot be used within square brackets for test purposes.

1.1.2 Integer comparison operations

1.1.2.1 -eq, -gt, etc.

In Test and [], integer comparison can use forms such as -eq and -gt.

1.1.2.1.1 in bash

[csdn ~]$ i=1; let i++; echo i=$i; j=12;let j--;echo j=$j 
i=2
j=11
[csdn ~]$ [ $i -lt $j ]
[csdn ~]$ echo $?
0
[csdn ~]$ [ $i -gt $j ]
[csdn ~]$ echo $?
1

1.1.2.1.2 in zsh

csdn @ edu $ i=1; let i++; echo i=$i; j=12;let j--;echo j=$j     
i=2
j=11
csdn @ edu $ [ $i -lt $j ]                                   
csdn @ edu $ echo $?
0
csdn @ edu $ [ $i -gt $j ]
csdn @ edu $ echo $?
1

 

 1.1.2.2 < and >

In most programming languages, can we directly use the greater than sign (>) and less than sign (<) for integer comparison in the Linux shell?

1.1.2.1.1 in bash

[csdn ~]$ i=1; let i++; echo i=$i; j=12;let j--;echo j=$j 
i=2
j=11 
[csdn ~]$ echo $[ i > j ]
0
[csdn ~]$ echo $[ i < j ]
1
[csdn ~]$ echo $[ i \< j ]
bash: i \< j : syntax error: invalid arithmetic operator (error token is "\< j ")
[csdn ~]$ [ i \< j ]
[csdn ~]$ echo $?
0
[csdn ~]$ [ i \> j ]
[csdn ~]$ echo $?
1

In the above example, we defined integer variables i and j,

First, use > and < directly to compare i and j. The results are 0 and 1 respectively. It is correct to compare the results as strings, but it is wrong to compare them as integers.

Then we use \< and \> to compare i and j, and the results are 0 and 1 respectively, which is correct.

The following is the result of comparing by string.

[csdn ~]$ i='2'; j="11"
[csdn ~]$ echo $[ i > j ]
0
[csdn ~]$ echo $[ i < j ]
1

1.1.2.1.2 in zsh

csdn @ edu $ i=1; let i++; echo i=$i; j=12;let j--;echo j=$j 
i=2
j=11
csdn @ edu $ echo $[ j > j ] 
0
csdn @ edu $ echo $[ j < j ] 
0
csdn @ edu $ echo $[ j \< j ] 
zsh: bad math expression: illegal character: \
csdn @ edu $ echo $[ j \> j ] 
zsh: bad math expression: illegal character: \

 

In the above example, we also defined integer variables i and j,

First, use > and < directly to compare i and j. The results are both 0, indicating a problem with the display.

Then we use \< and \> to compare i and j, but zsh does not support it.​ 

1.1.3 Logical operations

  • The logical AND in [ ] uses -a to express (and), and the logical OR uses -o to express (or).
  • The logical AND between [ ] uses && to express (and), and the logical or uses || to express (or).

1.1.3.1 in bash

[csdn ~]$ s1='a';s2='b';s3='a'
[csdn ~]$ [ $s1 == $s2 -o $s1 == $s3 ]
[csdn ~]$ echo $?
0
[csdn ~]$ [ $s1 == $s2 ] || [ $s1 == $s3 ]
[csdn ~]$ echo $?
0
[csdn ~]$  [ $s1 == $s2 -a $s1 == $s3 ]
[csdn ~]$ echo $?
1
[csdn ~]$ [ $s1 == $s2 ] && [ $s1 == $s3 ]
[csdn ~]$ echo $?
1

In the above example, we first defined three string variables: s1='a';s2='b';s3='a'

Then execute [ $s1 == $s2 -o $s1 == $s3 ], the result of s1 == $s2 is 1, and the result of $s1 == $s3 is 0. Since we are using -o (or), so The result is 0

Then use the command echo $? to display the result: 0

Then [ $s1 == $s2 ] || [ $s1 == $s3 ] and [ $s1 == $s2 -o $s1 == $s3 ] are the same.

Then execute [ $s1 == $s2 -a $s1 == $s3 ], the result of s1 == $s2 is 1, and the result of $s1 == $s3 is 0. Since we are using -a (and), so The result is 1

Finally, use the command echo $? to display the result: 0

1.1.3.2 In zsh

csdn @ edu zsh $ s1='a';s2='b';s3='a'
csdn @ edu zsh $ [ $s1 == $s2 -o $s1 == $s3 ]
zsh: = not found
csdn @ edu zsh $ [ $s1 = $s2 -o $s1 = $s3 ] 
csdn @ edu zsh $ echo $?
0
csdn @ edu zsh $ [ $s1 == $s2 ] || [ $s1 == $s3 ]
zsh: = not found
csdn @ edu zsh $ [ $s1 = $s2 ] || [ $s1 = $s3 ] 
csdn @ edu zsh $ echo $?                         
0
csdn @ edu zsh $ [ $s1 = $s2 -o $s1 = $s3 ] 
csdn @ edu zsh $ echo $?                   
0
csdn @ edu zsh $ [ $s1 = $s2 ] && [ $s1 = $s3 ] 
csdn @ edu zsh $ echo $?                       
1

The above example is the same as in bash, but zsh does not support == , we changed it to = .

1.2 Reference each element in the array

For arrays, square brackets can be used to access array elements by subscript index.

1.2.1 In bash

 [csdn ~]$ a=(1 2 3)
[csdn ~]$ echo ${a[1]} 1 2 3 [csdn ~]$ echo ${a[*]}
2

In the above example, we first define the array a=(1 2 3)

Then use the command echo ${a[1]} to display the second element in the array. The array subscript in bash starts from 0.

Then use the command echo ${a[*]} to display all elements in the array.

1.2.2 In zsh

csdn @edu zsh $ a=(1 2 3)
csdn @edu zsh $ echo $a[1]
1
csdn @edu zsh $ echo $a[*]
1 2 3 1 2 3 csdn @edu zsh $ echo ${a[*] } 1
csdn @edu zsh $ echo ${a[1]}


In the above example, we first define the array a=(1 2 3)

Then use the command echo ${a[1]} to display the first element in the array. The array subscript in bash starts from 1.

Then use the command echo ${a[*]} to display all elements in the array.

2 Double square brackets (square brackets) [[]]

[[ ]] is a Shell built-in keyword. Its function is similar to the [] or test command. It is also used to check whether a certain condition is true.

 [[ ]] is an upgraded version of [] or test, with details optimized and some functions expanded.

  • What [] or test can do, [[ ]] can also do, and [[ ]] can do it better;
  • What [] or test cannot do, [[ ]] can also do.

2.1 [[ ]] supports string, integer comparison operations and logical operations

2.1.1 In bash

[csdn ~]$ s1='a';s2='b';s3='a'
[csdn ~]$ [ $s1 = $s2 || $s1 = $s3 ]
bash: [: missing `]'
bash: a: command not found
[csdn ~]$ [[ $s1 = $s2 || $s1 = $s3 ]]
[csdn ~]$ echo $?
0
[csdn ~]$ [[ $s1 = $s2 -o $s1 = $s3 ]]
bash: syntax error in conditional expression
bash: syntax error near `-o'
[csdn ~]$ [ $s1 = $s2 && $s1 = $s3 ]
bash: [: missing `]'
[csdn ~]$ [[ $s1 = $s2 && $s1 = $s3 ]]
[csdn ~]$ echo $?
1
[csdn ~]$ [[ $s1 = $s2 -a $s1 = $s3 ]]
bash: syntax error in conditional expression
bash: syntax error near `-a'

2.1.2 In zsh

csdn @ edu zsh $ s1='a';s2='b';s3='a'        
csdn @ edu zsh $ [ $s1 = $s2 || $s1 = $s3 ]  
[: ']' expected
zsh: command not found: a
csdn @ edu zsh $ [[ $s1 = $s2 || $s1 = $s3 ]]
csdn @ edu zsh $ echo $?
0
csdn @ edu zsh $ [[ $s1 = $s2 -o $s1 = $s3 ]]
zsh: condition expected: $s1
csdn @ edu zsh $ [ $s1 = $s2 && $s1 = $s3 ] 
[: ']' expected
csdn @ edu zsh $ [[ $s1 = $s2 && $s1 = $s3 ]]
csdn @ edu zsh $ echo $?
1
csdn @ edu zsh $ [[ $s1 = $s2 -a $s1 = $s3 ]]
zsh: condition expected: $s1

&& and || are supported in [[ ]] , but -o and -a are not supported.​ 

2.2 Support regular expressions

In Shell [[ ]], you can use =~ to check whether a string matches a regular expression. Its usage is:

[[ string =~ regular expression ]]

For example:

  • ^[0-9]*$ is a regular expression to detect whether it is a pure numeric string
  • ^[a-z]*$ is a regular expression that detects whether it is a pure lowercase letter string

 

2.2.1 In bash

[csdn ~]$ s="12345"; [[ $s =~ ^[0-9]*$ ]]; echo $?
0
[csdn ~]$ s="12345abc"; [[ $s =~ ^[0-9]*$ ]]; echo $?
1
[csdn ~]$ s="12345"; [[ $s =~ ^[a-z]*$ ]]; echo $?
1
[csdn ~]$ s="abc"; [[ $s =~ ^[a-z]*$ ]]; echo $?
0

2.2.2 In zsh

csdn @ edu zsh $ s="12345"; [[ $s =~ ^[0-9]*$ ]]; echo $?   
0
csdn @ edu zsh $ s="12345abc"; [[ $s =~ ^[0-9]*$ ]]; echo $?
1
csdn @ edu zsh $ s="12345"; [[ $s =~ ^[a-z]*$ ]]; echo $?
1
csdn @ edu zsh $ s="abc"; [[ $s =~ ^[a-z]*$ ]]; echo $?
0

 

2.2.3 Note

To ensure that [[]] achieves correct results, the variable names must be prefixed with $.

In bash:

[csdn ~]$ s="12345"; [[ s =~ ^[0-9]*$ ]]; echo $?
1
[csdn ~]$ s="12345"; [[ $s =~ ^[0-9]*$ ]]; echo $?
0
[csdn ~]$ s="abc"; [[ s =~ ^[0-9]*$ ]]; echo $?
1
[csdn ~]$ s="abc"; [[ $s =~ ^[0-9]*$ ]]; echo $?
1
[csdn ~]$ s="abc"; [[ s =~ ^[a-z]*$ ]]; echo $?
0
[csdn ~]$ s="abc"; [[ $s =~ ^[a-z]*$ ]]; echo $?
0
[csdn ~]$ s="123"; [[ s =~ ^[a-z]*$ ]]; echo $?
0
[csdn ~]$ s="123"; [[ $s =~ ^[a-z]*$ ]]; echo $?
1

 

Guess you like

Origin blog.csdn.net/Purpleendurer/article/details/134308917