我对以下脚本感到非常疯狂。
以下命令按预期工作:
echo a | sed 's/a/b/'
输出 :
b
但是这个脚本没有:
test="'s/a/b/'"
echo a | sed $test
输出 :
sed: -e expression #1, char 1: unknown command : `''
我真的应该很愚蠢,但我看不出我错过了什么。
谢谢,
test="'s/a/b/'"
echo a | sed $test
相当于:
test="'s/a/b/'"
echo a | sed "'s/a/b/'"
显然sed
不理解带有"
and的命令'
,它解释'
为命令。您可以使用其中任何一种:
test='s/a/b/'
或者
test='s/a/b/'
你可能想要这个:
kent$ test="s/a/b/"
kent$ echo a | sed ${test}
b
或者
kent$ echo a | sed $test
b
或者
test=s/a/b/
这是因为你的双重包装你的字符串。test="'s/a/b'"
. 然后 Sed 得到's/a/b/'
文字字符串。您只希望 sed 接收s/a/b/
.
您只需要将字符串包装在一组引号中,否则内部引号将被解释为参数的一部分。