[英]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.