执行包含shell变量引号的命令 [英] Execute command containing quotes from shell variable
本文介绍了执行包含shell变量引号的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前在从shell变量执行命令时遇到问题.
I'm currently having problems to execute a command from a shell variable.
通常,以下功能可以正常工作:
In general the following works as expected:
COMMAND="echo A"
echo $COMMAND
$COMMAND
产生:
echo A
A
但如果我这样做
COMMAND="su aUser -s /bin/bash -c 'echo A'"
echo $COMMAND
$COMMAND
我知道
su aUser -s /bin/bash -c 'echo A'
Password:
A': -c: line 0: unexpected EOF while looking for matching `''
A': -c: line 1: syntax error: unexpected end of file
如果我输入这一行
su aUser -s /bin/bash -c 'echo A'
直接按预期工作.
似乎我认为$ COMMAND等于直接以命令形式输入内容是错误的.
It seems my assumption that $COMMAND is equal to entering the content as command directly is wrong.
1)有人知道我如何从变量中运行命令吗?
1) Does anyone know how I can run the command from a variable?
2)两者之间到底有什么区别
2) What exactly is the difference between
COMMAND="command"
$COMMAND
和
command
?
推荐答案
数组对于保持参数完整很有用:
Arrays are useful to keep your parameters whole:
command=(su aUser -s /bin/bash -c 'echo A')
并完全像这样调用它:
"${command[@]}"
这篇关于执行包含shell变量引号的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文