jor*_*anm 21
BASHPIDbash 联机帮助页的描述中提供了一个示例:
BASHPID
Expands to the process id of the current bash process. This
differs from $$ under certain circumstances, such as subshells
that do not require bash to be re-initialized.
Run Code Online (Sandbox Code Playgroud)
这是一个子shell的示例,它输出变量$$的内容以及BASHPID子shell外部的内容。
$ echo $(echo $BASHPID $$) $$ $BASHPID
25680 16920 16920 16920
# | | | |
# | | | -- $BASHPID outside of the subshell
# | | -- $$ outside of the subshell
# | -- $$ inside of the subshell
# -- $BASHPID inside of the subshell
Run Code Online (Sandbox Code Playgroud)
orm*_*aaj 15
子壳。$$由 POSIX 指定并始终保持原始 shell 进程的值。$BASHPID是 Bash 特定的变量,并且始终是取消引用该变量的进程的值,计算子 shell。
$ f() { printf '%s: %d, %d\n' "$1" $$ $BASHPID; };
$ ${BASH_VERSION+shopt -s lastpipe}; set +m;
$ f 1 >&2 | f 2
2: 31490, 31490
1: 31490, 32545
Run Code Online (Sandbox Code Playgroud)
我确实设法说服 mksh 维护者添加BASHPID到最新版本,所以它有点便携。也可以BASHPID自己在ksh93中在很多平台上实现。
| 归档时间: |
|
| 查看次数: |
5024 次 |
| 最近记录: |