Conditional test statement in bash

Source: Internet
Author: User
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 ~] #
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.