The condition test statement in bash-general Linux technology-Linux technology and application information. The following is a detailed description. [I = s] This post was last edited by yanyongkg at, 2011-8-13
Comparison string: ["string1" = "string2"] is equivalent to ["string1" = "string2"]
Compare integers: ["num1"-eq "num2"] is equivalent to ("num1" = "num2 "))
If the variable value is not empty and the variable value does not contain spaces, double quotation marks are not added to the variable. Of course, double quotation marks are also supported.
When the variable value is null or the variable value contains spaces, double quotation marks must be added to the variable.
Or the variable has no declaration, such as unset var, reference $ var, you must add double quotation marks
Therefore, adding double quotation marks to variables is a wise choice.
[<...> -A <...>] it is equivalent to [<...>] & [<...>] it is equivalent to [[<...> & <...>]
[<...> -O <...>] it is equivalent to [<...>] | [<...>] it is equivalent to [[<...> | <...>]
For the reason why spaces must be left on both sides of = in [], use the following example to describe:
# [A = B] & echo OK
OK
Why does it print OK? Bash understands a = B as a value assignment operation, and the value assignment operation is always true.
# [A = B] & OK
OK
Similarly, a = B can be understood as a = "= B" value assignment operation.
Example Series 1 (the expression of the following test statements is correct ):
Var = 3
["$ Var"-eq "3"]
("$ Var" = "3 "))
["$ Var"-eq 3]
[$ Var-eq 3]
["$ Var" = "3"] it is also possible to compare the integer var as a string.
["$ Var" = "3"] it is also possible to compare the integer var as a string.
[$ Var = 3]
["$ Var" = 3]
[$ Var = "3"]
Example Series 2 (the variable is null ):
Three methods for setting the null value variable $ var
Var = enter var = and press enter directly. Of course, if there are several more spaces followed and then press enter, bash automatically ignores spaces during execution.
Var = ''two single quotes
Var = "" or two double quotes
CODE: [root @ localhost ~] # Var = [Root @ localhost ~] # [$ Var = 1] & echo OK -Bash: [: =: unary operator expected [Root @ localhost ~] # [$ Var ''='' 1] & echo OK # The system prompts that space is required for the second or second sign, and spaces are added to quotation marks. OK # obviously $ var does not add double quotation marks, and the result is incorrect [Root @ localhost ~] # ["$ Var" = 1] & echo OK [Root @ localhost ~] # ["$ Var" = "1"] & echo OK [Root @ localhost ~] # |