bash的PID和$$的区别 [英] Difference between bash pid and $$
问题描述
我是bash脚本编程初学者,我有一个功课要做。我想大部分的东西出来,但有它说,我有呼应父的bash的PID和我将运行两个子shell的pid的一部分。所以,我在网上看了一下,发现这(Linux文档计划):
I'm a bash scripting beginner, and I have a "homework" to do. I figured most of the stuff out but there is a part which says that I have to echo the pid of the parent bash and the pid of the two subshells that I will be running. So I looked online and found this (The Linux documentation project):
#!/bin/bash4
echo "\$\$ outside of subshell = $$" # 9602
echo "\$BASH_SUBSHELL outside of subshell = $BASH_SUBSHELL" # 0
echo "\$BASHPID outside of subshell = $BASHPID" # 9602
echo
( echo "\$\$ inside of subshell = $$" # 9602
echo "\$BASH_SUBSHELL inside of subshell = $BASH_SUBSHELL" # 1
echo "\$BASHPID inside of subshell = $BASHPID" ) # 9603
# Note that $$ returns PID of parent process.
因此,这里是我的问题:
So here are my questions:
1)什么做的第一回波打印?这是父的bash的PID?
1) What does the first echo print? Is this the pid of the parent bash?
2)为什么第二回声打印出0?
2) Why does the 2nd echo print out 0?
3)是$ BASH_SUBSHELL一个命令或变量?
3) Is $BASH_SUBSHELL a command or a variable?
4)我在Mac上做的一切,我会尝试所有这一切都在Linux机器上一些日子,但
每当我运行此脚本 $ BASHPID
不返回任何东西,我只是得到一个新行。这是因为我在Mac上运行此和 $ BASHPID
在Mac上不起作用?
4) I'm doing everything on a mac, I will try all of this on a Linux machine in some days but
whenever I run this script $BASHPID
doesn't return anything, I just get a new line. Is this because I'm running this on a mac and $BASHPID
doesn't work on a mac?
推荐答案
看着这个文档 ,它看起来像:
Looking at documentation on this, it looks like:
- $$意味着该脚本文件下运行的进程ID。对于任何给定的脚本,在运行时,它会只有一个主进程ID。不管你有多少子shell调用,$$总是返回与脚本关联的第一个进程ID。 BASHPID会告诉你的bash的当前实例的进程ID,因此在子shell会比可能已调用它的顶级的bash不同。
- BASH_SUBSHELL表示子shell级别你。如果你在任何子shell水平不是,你的水平是零。如果你的主要程序启动一个子shell,即子shell级为1,如果你启动子shell中的一个子shell,级别为2,依此类推。
- BASH_SUBSHELL是一个变量。
- 也许BASHPID不是由你在bash的版本不支持?我怀疑这是一个陆委会的问题。
这篇关于bash的PID和$$的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!