[英]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
所以当你执行命令时
$ 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.