Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 为什么单引号和双引号在;bash-c";?_Linux_Bash_Shell - Fatal编程技术网

Linux 为什么单引号和双引号在;bash-c";?

Linux 为什么单引号和双引号在;bash-c";?,linux,bash,shell,Linux,Bash,Shell,在运行shell脚本之前使用bash-c选项生成新shell时, 我遇到了意外的动作。 当我使用单引号时,会生成新的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 但是,双引号没有 $ bash -c "echo pid is $$" pid is 2426 $

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

当我使用单引号时,会生成新的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
但是,
双引号
没有

$ 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为$$
,其中
$$$
与任何字符串一样。如果命令现在是
bash
,$$具有特殊含义

$ bash -c 'echo pid is $$'

现在,您得到的是已执行命令bash的PID,而不是交互式shell的PID。

因此,当您执行该命令时

$ command "echo pid is $$"
双引号确保命令
command
在完成所有替换的位置传递字符串。假设交互式shell的pid为1234。你会得到同等的报酬

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

$ bash -c 'echo pid is $$'

现在您得到的是已执行命令
bash
返回的PID,而不是交互式shell返回的PID。

非现场但非网络:不是我的否决票,顺便说一句。@JamesBrown谢谢。这就是我想要的答案!说
manbash
,然后输入
/QUOTING
场外但不是网络:顺便说一句,不是我的否决票。@JamesBrown谢谢。这就是我想要的答案!说
manbash
并输入
/QUOTING