简体   繁体   English

为什么单引号和双引号在“bash -c”中的工作方式不同?

[英]why single quotes and double quotes works differently in “bash -c”?

While using bash -c option to spawn new shell before running shell script,在运行 shell 脚本之前使用bash -c选项生成新的 shell 时,
I encountered unexpected action.我遇到了意想不到的动作。

When I use single quotes , new shell is spawned to run the script.当我使用single quotes时,会生成新的 shell 来运行脚本。

$ bash -c 'echo pid is $$'
pid is 53465
$ bash -c 'echo pid is $$'
pid is 53466
$ bash -c 'echo pid is $$'
pid is 53477

But double quotes didn't.double quotes没有。

$ bash -c "echo pid is $$"
pid is 2426
$ bash -c "echo pid is $$"
pid is 2426
$ bash -c "echo pid is $$"
pid is 2426

I carefully read similar question and bash manual but could not find why.我仔细阅读了类似的问题bash 手册,但找不到原因。
Anyone knows the reason why?有人知道原因吗?

So when you execute the command所以当你执行命令时

$ command "echo pid is $$"

The double quotes ensure that the command command gets a string passed where all substitutions are done.双引号确保命令command在所有替换都完成的地方得到一个传递的字符串。 So assuming that the pid of the interactive shell is 1234. You will get the equivalent所以假设交互式 shell 的 pid 是 1234。你会得到等价的

$ command "echo pid is 1234"

When you use single quotes, the command gets passed the string echo pid is $$ where $$ is just like any string.当您使用单引号时,命令会传递字符串echo pid is $$其中$$就像任何字符串一样。 If the command is now bash , $$ has a special meaning如果命令现在是bash , $$ 有特殊含义

$ bash -c 'echo pid is $$'

So now you get the PID returned of the executed command bash and not of your interactive shell.所以现在你得到了执行命令bash而不是你的交互式 shell 返回的 PID。

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

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