bash pid 和 $$ 之间的区别
我是 bash 脚本初学者,我有一个“作业”要做。我弄清楚了大部分内容,但有一部分说我必须回显父 bash 的 pid 和我将运行的两个子 shell 的 pid。所以我上网查了一下,发现了这个(Linux文档项目):
#!/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.
所以这里是我的问题:
1)第一个回显打印什么?这是父bash的pid吗?
2)为什么第二个echo打印出0?
3) $BASH_SUBSHELL 是命令还是变量?
4)我在 Mac 上做所有事情,过几天我会在 Linux 机器上尝试所有这些,但是 每当我运行此脚本时 $BASHPID
不会返回任何内容,我只是得到一个新行。这是因为我在 Mac 上运行它,而 $BASHPID
在 Mac 上不起作用吗?
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) What does the first echo print? Is this the pid of the parent bash?
2) Why does the 2nd echo print out 0?
3) Is $BASH_SUBSHELL a command or a variable?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看文档,它看起来像:
$$ 表示脚本文件正在运行的进程 ID。对于任何给定的脚本,当它运行时,它将只有一个“主”进程 ID。无论您调用多少个子 shell,
$$
都将始终返回与脚本关联的第一个进程 ID。BASHPID
将显示当前 bash 实例的进程 ID,因此在子 shell 中它将与可能调用它的“顶级”bash 不同。BASH_SUBSHELL
表示您所在的“子 shell 级别”。如果您不处于任何子 shell 级别,则您的级别为零。如果您在主程序中启动一个子 shell,则该子 shell 级别为 1。如果您在该子 shell 中启动一个子 shell,则该级别将为 2,依此类推。BASH_SUBSHELL
是一个变量。BASHPID
?我怀疑这是一个“Mac”问题。Looking at documentation on this, it looks like:
$$
means the process ID that the script file is running under. For any given script, when it is run, it will have only one "main" process ID. Regardless of how many subshells you invoke,$$
will always return the first process ID associated with the script.BASHPID
will show you the process ID of the current instance of bash, so in a subshell it will be different than the "top level" bash which may have invoked it.BASH_SUBSHELL
indicates the "subshell level" you're in. If you're not in any subshell level, your level is zero. If you start a subshell within your main program, that subshell level is 1. If you start a subshell within that subshell, the level would be 2, and so on.BASH_SUBSHELL
is a variable.BASHPID
isn't supported by the version of bash you have? I doubt it's a "Mac" problem.最好熟悉
bash(1)
:$BASHPID
是通过 bash-4.0-alpha。如果您运行bash --version
,您可以了解您正在使用的bash(1)
版本。如果您要执行大量
bash(1)
工作,您还需要以下内容:It'd be best to get well-acquainted with
bash(1)
:$BASHPID
was introduced with bash-4.0-alpha. If you runbash --version
you can find out what version ofbash(1)
you're using.If you're going to be doing much
bash(1)
work, you'll also need the following: