我想在shell脚本中执行以下命令
CMD="/abc/def/ghi command1 command2 -p $VARIABLE --command3 true"
执行此exec $CMD
连字符替换为单引号粘贴调试输出
+ exec '/abc/def/ghi command1 command2 ' 'p VALUE ' '' 'command3 true'
如何处理?
答案 0 :(得分:0)
阅读I'm trying to put a command in a variable, but the complex cases always fail!
您想使用数组来存储命令
cmd=(/abc/def/ghi command1 command2 -p "$variable" --command3 true)
exec "${cmd[@]}"
或者是一个功能,但我不确定如何exec
一个函数,除非你把它放在函数中
cmd() { exec /abc/def/ghi command1 command2 -p "$variable" --command3 true; }
cmd
不要使用ALL_CAPS_VARNAMES:保留为shell保留的内容。