shell脚本$$值

当我使用这些shell命令时:

[root@linux /tmp]# a=$$ [root@linux /tmp]# echo $a 3985 

价值3985从哪里来? 为什么?

 man bash 

解释它。

 Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell. 

$$是当前进程的pid

尝试

主机@控制器:〜$ echo $$
 12481
主机@控制器:〜$ ps -p 12481
   PID TTY TIME CMD
 12481 pts / 2 00:00:01 bash
主持人@控制器:〜$

由于我们在bash中执行echo $$ ,所以我们得到它是当前的pid

也知道

echo $? 是上次执行的命令的返回码。

$#是参数的数量

$ *是传递给当前进程的参数列表

$ [1或2或… n]每个相应参数的值

这就是为什么有些人使用它来构造一个只能暂时使用,然后被销毁的文件名,就像这个脚本片段一样。

 SCRATCHFILE=/tmp/emphemeral.$$ ; # Now have the script write to and read from the temp file rm $SCRATCHFILE ; 

如上所述,文件名中的$$将是主脚本的PID。

尝试“回声$$”,你会得到答案。