The assignment statement and Judgment statement in parentheses judgment in shell programming

Source: Internet
Author: User

#!/bin/bashdeclare var="XXX" #without space and use one =#1.judge Whether the assignment statement returns trueEcho"----------------------test Assignment in bracket--------------------------------"[var="yyyy"] && Echo"Success"Echo"End"    #Success["$var"="yyyy"] && Echo"Success"Echo"End"    #Success["var"="yyyy"] && Echo"Success"Echo"End"    #Success[var1="yyyy"] && Echo"Success"Echo"End"    #Success#Result:yes#2. Judge the assignment whether is taken in bracketEcho"--------------------Test Assignment whether been taken-----------------------"[var="yyyy"] && Echo"$var"Echo"End"    #XXX    #End#result:assignment can ' t be taken in bracket#3. Whether the effect of single = and double = SimilaryEcho"-------------------Test single = and double =------------------------"[ "$var"=="yyyy"] && Echo"$var"Echo"End"    #End["$var"="yyyy"] && Echo"$var"Echo"End"    #EndVar="yyyy"[ "$var"=="yyyy"] && Echo"$var"Echo"End"    #yyyy    #End["$var"="yyyy"] && Echo"$var"Echo"End"    #yyyy    #End#Result:effect Same#4. Whether the effect of have space and not space similaryEcho"----------------------Test Space-------------------------------------"[ "$var"=="yyyy"] && Echo"$var"Echo"End"    #yyyy    #End["$var"=="zzzz"] && Echo"$var"Echo"End"    #yyyy    #End#result:always True with no space#Conclusion:#With space at both side of = would judge in normal#don ' t space at both side of = or = = would always true

The conclusions obtained from the above tests are as follows:

Do not use spaces such as "$var" = "xx" or "$var" = = "xxx" (no spaces on both sides of = and = =) in the judgment of brackets.

Use spaces such as "$var" = "xx" or "$var" = = "xxx" in brackets (there are spaces on both sides of = and = =), the judgment will be judged by the normal value, and the value is equal to return true otherwise false

May vary depending on the shell used and its version, the shell I use is: GNU bash, version 4.1.2 (1)-release (I686-REDHAT-LINUX-GNU)

The assignment statement and Judgment statement in parentheses judgment in shell programming

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.