bash – 带空格的Shell变量,引用单个命令行选项

Autoconf脚本在使用空格的文件名或路径名时遇到问题.例如,
./configure CPPFLAGS="-I\"/path with space\""

结果(config.log):

configure:3012: gcc  -I"/path with space"  conftest.c  >&5
gcc: with: No such file or directory
gcc: space": No such file or directory

来自./configure的编译命令是ac_compile =’$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext>& 5′,我无法修改这个(我也许可以,但是以这种方式处理autoconf不是一般的解决方案).

我认为它得到一个shell变量,其中包含要解析为单个命令行变量而不是在空格处拆分的空格.我可以想出的最简单的shell示例是创建一个带有空格的文件,并尝试列出一个带有一个shell变量的ls作为ls的参数:

$touch "a b"
$file="a b"
$ls $file
ls: a: No such file or directory
ls: b: No such file or directory

这个工作,但是是非法的,因为在autoconf中我无法修改shell代码

$ls "$file"
a b

以下尝试引用的事情都不行:

$file="\"a \"b"; ls $file
ls: "a: No such file or directory
ls: b": No such file or directory
$file="a\ b"
$file="a\\ b"
$file="`echo \\"a b\\"`"

等等.

这是不可能在shell脚本中完成的吗?有没有一个神奇的引用,将扩展一个shell变量与空格到一个命令行参数?

您应该尝试设置$IFS环境变量.

从男子bash(1):

IFS – The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read builtin
command. The default value is ”space tab newline”.

例如

IFS=<C-v C-m>  # newline
file="a b"
touch $file
ls $file

不要忘记设置$IFS回来或奇怪的事情会发生.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...