In some ancient Shell scripts for Solaris I found the following notation to check if a variable is empty:
[ x"$var" = x ]
On a current Linux system I would write it just as
[ "$var" = "" ]
or
[ -z "$var" ]
Which version of [
actually requires the first notation?
None.The alias from [] to test appears after the option -z of test. Therefore, if you can use [ x"$var" = x]
, you can also write [ -z "$var" ]
.
来源:https://stackoverflow.com/questions/35365744/which-can-not-compare-empty-strings