我对在子shell中引用、参数和glob扩展的工作原理感到困惑。子shell命令行的引用和扩展是否始终发生在子shell进程的上下文中?我的测试似乎证实了这一点。
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ ls
a b c
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ echo "$(echo *)"
a b c
# The subshell expands the * glob
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ echo $(echo '*')
a b c
# The subshell outputs literal *, parent shell expands the * glob
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ echo $(echo "*")
a b c
# The subshell outputs literal *, parent shell expands the * glob
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ echo "$(echo '*')"
*
# The subshell outputs literal *, parent shell outputs literal *
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ foo=bar
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ echo "$(echo $foo)"
bar
# The subshell expands variable foo
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ echo $(echo '$foo')
$foo
# The subshell outputs literal $foo
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ echo $(echo "$foo")
bar
# The subshell expands variable foo
Tuomas@DESKTOP-LI5P50P MINGW64 ~/shell/test1/test
$ echo "$(echo '$foo')"
$foo
# The subshell outputs literal $foo
我理解您的意思是:父进程会在分叉子进程之前处理或评估子shell命令行的情况是否存在,我的理解正确吗?
echo "$BASHPID"
与echo "$(echo "$BASHPID)")"
进行比较。 - Charles Duffy