Why do conditionals in autoconf scripts prefix variables with “x”?

前端 未结 2 807
南方客
南方客 2020-12-18 04:29

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

相关标签:
2条回答
  • 2020-12-18 05:03

    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...

    0 讨论(0)
  • 2020-12-18 05:14

    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.

    0 讨论(0)
提交回复
热议问题