#!/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