Why do shell script comparisons often use x$VAR =

2019-01-04 00:29发布

I see this often in the build scripts of projects that use autotools (autoconf, automake). When somebody wants to check the value of a shell variable, they frequently use this idiom:

if test "x$SHELL_VAR" = "xyes"; then
...

What is the advantage to this over simply checking the value like this:

if test $SHELL_VAR = "yes"; then
...

I figure there must be some reason that I see this so often, but I can't figure out what it is.

7条回答
Root(大扎)
2楼-- · 2019-01-04 01:11

If you don't do the "x$SHELL_VAR" thing, then if $SHELL_VAR is undefined, you get an error about "=" not being a monadic operator or something like that.

查看更多
登录 后发表回答