表面上的意思是: 当前bash的PID,但是各种shell环境中要理解透。
官方解释:
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.
输出当前bash进程的pid。这是不同的在以下的情况中,例如没有获取到bash的子shell,将被重新初始化。
例子1:
[[email protected] ~]$ ps -ef |grep bash |grep -v grep
andy 30245 30244 0 Jul17 pts/0 00:00:00 -bash
[[email protected] ~]$ echo $$
30245
注意: $$ 表示的是这个登入shell的进程ID(直接一andy用户ssh连接登入的shell)
[[email protected] ~]$
例子2:
[[email protected] std]# ps -ef |grep bash
root 4422 4421 0 Jul23 pts/0 00:00:02 -bash
root 13791 4422 0 22:14 pts/0 00:00:00 grep --color=auto bash
andy 30245 30244 0 Jul17 pts/0 00:00:00 -bash
[[email protected] std]# echo $$
4422
注意:$$表示的是登入交互式shell的PID(我本机是andy登入,然后sudo su - 到root的)
例子3:
[[email protected] std]# echo $$
4422
[[email protected] std]# cat test.sh
#/bin/bash
echo "fei jiao hu shi shell‘S process pid:$$"
[[email protected] std]# sh test.sh
fei jiao hu shi shell‘S process pid:13858
注意:这是非交互式shell,通过脚本执行的shell,$$ 就是这个pid
例子4:
[[email protected] ~]$ echo $$
30245
[[email protected] ~]$ echo "abc" | { echo "abc"; echo $$; }
abc
30245
[[email protected] ~]$
注意: 管道中开辟的subshell,虽然是子shell环境,但是没有获得-bash进程的,所以依然是父进程的bashID。(这个有点绕,看看官方解释的那段英文就慢慢理解吧。)