code_junkie

Why do shell script comparisons often use x$VAR = xyes?

Nov 11th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. if test "x$SHELL_VAR" = "xyes"; then
  2. ...
  3.  
  4. if test $SHELL_VAR = "yes"; then
  5. ...
  6.  
  7. if test $SHELL_VAR = yes; then --> if test = yes; then
  8. if test x$SHELL_VAR = xyes; then --> if test x = xyes; then
  9.  
  10. if test "x$SHELL_VAR" = "xyes"; then --> if test "x" = "xyes"; then
  11.  
  12. if test "$1" = "abc" ; then ...
  13.  
  14. if [ "$1" = "abc" ]; then ...
  15.  
  16. if [ -z "$SOME_VAR" ]; then
  17. echo "this variable is not defined"
  18. fi
  19.  
  20. SHELLVAR=$(true)
  21. if test $SHELLVAR = "yes" ; then echo "yep" ; fi
  22.  
  23. # bash: test: =: unary operator expected
  24.  
  25. if test $UNDEFINEDED = "yes" ; then echo "yep" ; fi
  26. # bash: test: =: unary operator expected
  27.  
  28. SHELLVAR=" hello"
  29. if test $SHELLVAR = "hello" ; then echo "yep" ; fi
  30. # yep
  31.  
  32. SHELLVAR=" hello"
  33. if test "$SHELLVAR" = "hello" ; then echo "yep" ; fi
  34. #<no output>
  35.  
  36. SHELLVAR=" hello"
  37. if test "x$SHELLVAR" = "xhello" ; then echo "yep" ; fi
Add Comment
Please, Sign In to add comment