use of string comparison X "$variable" in Shell
This article refers to StackOverflow why does shell script comparisons often use X$var = Xyes? do not use double quotes "", do not use prefix x do not use double quotes "", use prefix x use double quotation marks "" without using prefix x use the double quotation mark "", using the prefix x
Shell string comparison xvariable using double quotation marks without prefix x using double quotes without prefix x using double quotation marks with prefix x using double quotation marks X
do not use double quotes "", do not use prefix x
Variable Shell_var undefined ($SHELL _var is empty)
if test $SHELL _var = yes; Then--and if test = yes; Then
Obviously there is a syntax error, and the test loses the parameter. do not use double quotes "", use prefix x
$SHELL _var value is empty
if test X$shell_var = yes; Then--and if test x = yes; Then
It seems that there is no problem in looking at it.
However, if the $shell_var value is "yes" at this point, note that there is a space in front of Yes, then:
if test X$shell_var = yes; Then--and if test x Yes = yes; Then
Obviously this is also a syntax error, test has two parameters X and 1. use double quotation marks "" without using prefix x
$SHELL _var value is empty
if test "$SHELL _var" = "yes"; Then--and if test "=" yes "; Then
$SHELL _var value is "yes"
if test "$SHELL _var" = "yes"; Then--and if test "yes" = "yes"; Then
It seems that there is no problem in wrapping up variables with "".
But if the $shell_var value is "-N" or "-F" at this time
if test "$SHELL _var" = "yes"; Then--and if test "-F" = "yes"; Then
Then "-F" is a two semantic, which is either the option of the test command or the argument of test. use the double quotation mark "", using the prefix x
$SHELL _var value is empty
if test X "$SHELL _var" = x "yes"; Then--and if test X "" = x "yes"; Then
$SHELL _var value is "yes"
if test X "$SHELL _var" = x "yes"; Then--and if test X "yes" = x "yes"; Then
$SHELL _var value is "-N" or "-F"
if test X "$SHELL _var" = x "yes"; Then--and if Test X "-F" = x "yes"; Then