You are not logged in.
It may be a dumb question, but I couldn't find the answer. Are there any reasons to use
if [ "x${string}" != "x" ]
instead of
if [ -n "${string}" ]
or just
if [ "${string}" ]
?
As for the first syntax, I can see some disadvantages (it's longer, less readable, probably also less efficient) while no obvious advantages over the other ones. Is the first version more portable, or something like that?
I ask because I'm writing my own encryption hook for mkinitcpio (to add support for resuming from encrypted swap), and I've encountered this syntax in existing code. Now I wonder whether it's safe to always go the usual way in this hook...
Solved, see hbekel's answer
Last edited by Rad3k (2010-10-01 13:53:19)
Offline
"The x"$foo" hack is required for code that must run on ancient shells which lack [[, and have a more primitive [, which gets confused if $foo begins with a -."
Offline
Thanks hbekel
So portability (probably taken to the extreme) is the reason here. I wonder whether busybox's ash qualifies as such an ancient shell. Will have to check it. Marking as solved.
Offline
It's easier to port a shell than a shell script. -- Larry Wall
Offline
Offline