简体   繁体   English

在包含“-”(连字符)的 shell 脚本中执行命令

[英]Executing command in shell script which contain "-"(Hyphen)

I want execute below command in shell script我想在 shell 脚本中执行下面的命令

CMD="/abc/def/ghi command1 command2 -p $VARIABLE --command3 true"

while executing this exec $CMD hyphen replacing with single quotes pasting debug output在执行此exec $CMD连字符替换为粘贴调试输出的单引号时

+ exec '/abc/def/ghi command1 command2 ' 'p VALUE ' '' 'command3 true'

How to deal with it ?如何处理?

Read I'm trying to put a command in a variable, but the complex cases always fail!阅读我正在尝试将命令放入变量中,但复杂的情况总是失败!

You want to use an array to store the command您想使用数组来存储命令

cmd=(/abc/def/ghi command1 command2 -p "$variable" --command3 true)
exec "${cmd[@]}"

Or a function, but I'm not sure how to exec a function, unless you put it in the function或者一个函数,但是我不确定如何exec一个函数,除非你把它放在函数中

cmd() { exec /abc/def/ghi command1 command2 -p "$variable" --command3 true; }
cmd

Don't use ALL_CAPS_VARNAMES: leave those reserved for the shell.不要使用 ALL_CAPS_VARNAMES:为 shell 保留那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM