Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do conditionals in autoconf scripts prefix variables with "x"?

Tags:

autoconf

Why do conditional statements in autoconf scripts prefix their variables with "x"? For example, the macro provided by GNU to test for Boost has conditionals such as

if test "x$want_boost" = "xyes"; then

Why is this not defined as:

if test "$want_boost" = "yes"; then
like image 461
dfk22 Avatar asked Jun 29 '12 16:06

dfk22


2 Answers

In some early shells, testing for an empty string variable wasn't as easy as it is now, so the best alternative was to see if "x$variable" was equal to just "x". Also, since that's apparently using test, that's simpler than trying to properly quote/escape sequences like '$x != "y"' without losing sanity and/or portability.

like image 150
twalberg Avatar answered Oct 14 '22 02:10

twalberg


In POSIX-compliant shells,

test "$foo" = "$bar"

does a string comparison no matter what the contents of the variables foo and bar are. But in old, noncompliant shells, if foo contained something that begins with a dash, test would try to interpret that as a unary operator, so it would either throw a syntax error or do the wrong test. Writing x"$foo" = x"$bar" makes that impossible. I also vaguely recall problems if either argument expanded to the empty string, but that might have only been if you left out the double quotes (in which case it would be a problem with a modern shell, too).

This is particularly relevant when writing configure scripts, because, at least in principle, they're supposed to work no matter how old the shell environment is; and if you're writing a new program today, in 2020, the most plausible reason why you'd use C and autoconf is that you need portability to some old systems...

like image 33
zwol Avatar answered Oct 14 '22 02:10

zwol