了解 configure.ac 文件中的 bashism

Understanding a bashism in a configure.ac file

我试图了解项目 configure.ac 文件中的常见模式。我相当确定这是一个我很难解析的 bashism。这是一个例子:

LDFLAGS="${LDFLAGS:+$LDFLAGS }$OTHERFLAG"

其意图似乎是通过 OTHERFLAG 中的内容扩展 LDFLAGS 的当前值。似乎这样写会更简单:

LDFLAGS="$LDFLAGS $OTHERFLAG"

相关section in the bash manual说:

${parameter:+word}
    If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

我假设这里有一些防御性的东西,但我不确定到底是什么。

如果你写:

LDFLAGS="$LDFLAGS $OTHERFLAG"

并且 $LDFLAGS 未设置,它将具有以空 space 开头的字符串,这可能是不可取的。

这就是为什么 :

LDFLAGS="${LDFLAGS:+$LDFLAGS }$OTHERFLAG"

示例:

tiago@dell:~$ unset a ; b="test"; a="${a:+$a }$b"; echo "$a"
test
tiago@dell:~$ unset a ; b="test"; a="$a $b"; echo "$a"
 test