我对下面的脚本完全疯了。
以下命令按预期工作:
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
不理解"
和'
的命令,它将'
解释为命令。您可以使用其中之一:
test='s/a/b/'
或
test='s/a/b/'
这是因为您的双重包装了字符串。 test="'s/a/b'"
.然后,Sed 's/a/b/'
为文本字符串。您只希望 sed 接收s/a/b/
.
您只需要将字符串包装在一组引号中,否则内部引号集将被解释为参数的一部分。
你可能想要这个:
kent$ test="s/a/b/"
kent$ echo a | sed ${test}
b
或
kent$ echo a | sed $test
b
或
test=s/a/b/