我有一个简单的脚本,用于打印接收到的内容,然后退出:
如果我执行:
问题:
我有一个外部程序(无法修改)将其传递给我的脚本,但当执行时它会打印出以下内容:
IFS="|${IFS}";
echo "$# parameters";
echo Using '$*';
for p in $*;
do
echo "[$p]";
done;
echo Using '"$*"';
for p in "$*";
do
echo "[$p]";
done;
echo Using '$@';
for p in $@;
do
echo "[$p]";
done;
echo Using '"$@"';
for p in "$@";
do
echo "[$p]";
done
如果我执行:
./echoparams This is a "test target"
它将打印出如下内容:4 parameters
Using $*
[This]
[is]
[a]
[Test]
[target]
Using "$*"
[This|is|a|Test target]
Using $@
[This]
[is]
[a]
[Test]
[target]
Using "$@"
[This]
[is]
[a]
[Test target]
问题:
我有一个外部程序(无法修改)将其传递给我的脚本,但当执行时它会打印出以下内容:
1 parameters
Using $*
[This]
[is]
[a]
["test]
[target"]
Using "$*"
[This is a "test target"]
Using $@
[This]
[is]
[a]
["test]
[target"]
Using "$@"
[This is a "test target"]
我有一种预感,实际上它将 "This is a \"test target\""
传递到脚本中。 如何将这个“一个参数”变成“多个参数”,同时仍然尊重组合的单词(用引号括起来)?
eval
会留下四个参数,分别是 'This'、'is'、'a' 和 'test target' —— 但双引号会被移除,因为"test
和target"
被解释并组合在一起了。但eval
基本上已经是最好的选择了。 - Jonathan Leffler