Test for empty string with X""

Viewed 67548

I know I can test for an empty string in Bash with -z like so:

if [[ -z $myvar ]]; then do_stuff; fi

but I see a lot of code written like:

if [[ X"" = X"$myvar" ]]; then do_stuff; fi

Is that method more portable? Is it just historical cruft from before the days of -z? Is it for POSIX shells (even though I've seen it used in scripts targeting bash)? Ready for my history/portability lesson.


The same question was asked on Server Fault as How to determine if a bash variable is empty? but no one offered an explanation as to why you see code with the X"" stuff.

2 Answers
Related