bash如何处理嵌套引号? [英] How does bash deal with nested quotes?
问题描述
我需要运行一个带有这样语法的命令:
runuser -l userNameHere -c'/ path / to / command arg1 arg2'
I need to run a command with a syntax like this:
runuser -l userNameHere -c '/path/to/command arg1 arg2'
不幸的是,我必须在命令中嵌入额外的'
字符,我不能告诉bash正确解释这些。我想运行的命令实际上是:
Unfortunately, I have to nest additional '
characters into the command itself and I can't tell bash to interpret these correctly. The command I would like to run is actually:
runuser -l miner -c'screen -S Mine -p 0 -X不幸的是,bash似乎正在击中第二个
'
$ b $ code>和puking。这是错误:
-bash:screen -S Mine -p 0 -X eval stuff:没有这样的文件或目录
,所以显然不是通过'
。
runuser -l miner -c 'screen -S Mine -p 0 -X eval 'stuff "pwd"\015''
Unfortunately, bash seems to be hitting the second '
and puking. This is the error:
-bash: screen -S Mine -p 0 -X eval stuff: No such file or directory
, so obviously it's not getting past the '
.
如何嵌套为一个命令?谢谢!
How can I nest this as one command? Thank you!
推荐答案
您可以使用 bash
, $'...'
。这可以包含转义的单引号。
You can use another type of quoting supported by bash
, $'...'
. This can contain escaped single quotes.
runuser -l miner $'screen -S Mine -p 0 -X eval \'stuff "pwd"\015\''
请注意,在 $ '...'
,则会在代码点015处将 \015
替换为实际的ASCII字符,因此,如果这不是你想要的,你还需要转义反斜杠。
Note that within $'...'
, the \015
will be treated replaced with the actual ASCII character at codepoint 015, so if that's not what you want, you'll need to escape the backslash as well.
runuser -l miner $'screen -S Mine -p 0 -X eval \'stuff "pwd"\\015\''
可以利用 $'...'
来删除 eval
的需要:
I think you can take advantage of the $'...'
to remove the need for eval
as well:
runuser -l miner $'screen -S Mine -p 0 -X stuff "pwd"\015'
这篇关于bash如何处理嵌套引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!