开发者

Bash, exec and the quoting hell

开发者 https://www.devze.com 2023-04-01 00:33 出处:网络
How can I preserve single and double quotes when running a comm开发者_C百科and using exec? #!/bin/sh

How can I preserve single and double quotes when running a comm开发者_C百科and using exec?

#!/bin/sh

CMD="erl -eval 'erlang:display("foo")'"
exec $CMD

Tried with backslashes, but didn't help. For example, if I do what it would sound ovious to me:

#!/bin/sh

CMD="erl -eval 'erlang:display(\"foo\")'"
echo $CMD
exec $CMD

I get as output of the echo exactly what I want, but the command is not executed correctly when using exec.

I'm working on Snow Leopard.

Any help?


Try using an array:

CMD=(erl -eval 'erlang:display("foo")')
echo "${CMD[@]}"
"${CMD[@]}"


It will work if you use eval instead of exec, if this is the last thing in your script it won't make a huge difference operationally...

#!/bin/sh

CMD="erl -eval 'erlang:display(\"foo\")'"
echo $CMD
eval $CMD
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号