b2命令在直接调用时查找ICU,但不从间接bash脚本变量中查找

Bra*_*non 5 bash boost compilation

我的bash脚本有这个奇怪的问题.我编译boost作为其中的一部分.脚本调用如下所示:

./b2 --reconfigure ${PARALLEL} link=static cxxflags=-fPIC install boost.locale.iconv=off boost.locale.posix=off -sICU_PATH="${ICU_PREFIX}" -sICU_LINK="${BOOST_ICU_LIBS}" >> "${BOOST_LOG}" 2>&1
Run Code Online (Sandbox Code Playgroud)

该命令非常有效.日志文件显示它发现ICU没有问题.但是,如果我将其更改为从变量运行,它将不再找到ICU(但它仍会编译其他所有内容):

bcmd="./b2 --reconfigure ${PARALLEL} link=static cxxflags=-fPIC install boost.locale.iconv=off boost.locale.posix=off -sICU_PATH=\"${ICU_PREFIX}\" -sICU_LINK=\"${BOOST_ICU_LIBS}\""
$bcmd >> "${BOOST_LOG}" 2>&1
Run Code Online (Sandbox Code Playgroud)

有什么不同?我希望能够使用第二种方法,以便我可以在运行之前将命令传递给另一个函数.

sse*_*lla 0

eval当您想要将字符串作为命令执行时尝试使用。这样,您就不会遇到有关包含空格等的字符串的问题。扩展后的cmd字符串不会被重新评估bash,因此,类似的东西"hi there"会扩展为两个单独的标记。

eval "$bcmd" >> "${BOOST_LOG}" 2>&1
Run Code Online (Sandbox Code Playgroud)

为了演示此行为,请考虑以下代码:

cmd='echo "hi there"'
$cmd
eval "$cmd"
Run Code Online (Sandbox Code Playgroud)

哪个输出到:

"hi there"
hi there
Run Code Online (Sandbox Code Playgroud)

该标记"hi there"不会被重新评估为带引号的字符串。