繁体   English   中英

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

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

在运行 shell 脚本之前使用bash -c选项生成新的 shell 时,
我遇到了意想不到的动作。

当我使用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

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

我仔细阅读了类似的问题bash 手册,但找不到原因。
有人知道原因吗?

所以当你执行命令时

$ command "echo pid is $$"

双引号确保命令command在所有替换都完成的地方得到一个传递的字符串。 所以假设交互式 shell 的 pid 是 1234。你会得到等价的

$ command "echo pid is 1234"

当您使用单引号时,命令会传递字符串echo pid is $$其中$$就像任何字符串一样。 如果命令现在是bash , $$ 有特殊含义

$ bash -c 'echo pid is $$'

所以现在你得到了执行命令bash而不是你的交互式 shell 返回的 PID。

暂无
暂无

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

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