[英]Difference between bash pid and $$
I'm a bash scripting beginner, and I have a "homework" to do. 我是一个bash脚本初学者,我有一个“功课”要做。 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.
我想出了大部分的东西,但有一部分说我必须回应父bash的pid和我将要运行的两个子shell的pid。 So I looked online and found this (The Linux documentation project) :
所以我在网上找到了这个(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.
So here are my questions: 所以这是我的问题:
1) What does the first echo print? 1)第一个回声打印是什么? Is this the pid of the parent bash?
这是父母bash的pid吗?
2) Why does the 2nd echo print out 0? 2)为什么第二个回波打印出0?
3) Is $BASH_SUBSHELL a command or a variable? 3)$ BASH_SUBSHELL是命令还是变量?
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. 4)我正在Mac上做所有事情,我会在某些日子里在Linux机器上尝试所有这些但是每当我运行这个脚本时,
$BASHPID
都没有返回任何内容,我只是换了一个新行。 Is this because I'm running this on a mac and $BASHPID
doesn't work on a mac? 这是因为我在Mac上运行它并且
$BASHPID
在mac上不起作用吗?
Looking at documentation on this, it looks like: 看一下这方面的文档 ,它看起来像:
$$
means the process ID that the script file is running under. $$
表示运行脚本文件的进程ID。 For any given script, when it is run, it will have only one "main" process ID. $$
will always return the first process ID associated with the script. $$
都将返回与该脚本关联的第一个进程ID。 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. BASHPID
将显示当前bash实例的进程ID,因此在子shell中它将与可能已调用它的“顶级”bash不同。 BASH_SUBSHELL
indicates the "subshell level" you're in. If you're not in any subshell level, your level is zero. BASH_SUBSHELL
表示你BASH_SUBSHELL
的“子shell级别”。如果你没有任何子shell级别,你的级别为零。 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. BASH_SUBSHELL
是一个变量。 BASHPID
isn't supported by the version of bash you have? BASHPID
? I doubt it's a "Mac" problem. It'd be best to get well-acquainted with bash(1)
: 最好熟悉
bash(1)
:
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_SUBSHELL
Incremented by one each time a subshell or subshell
environment is spawned. The initial value is 0.
$BASHPID
was introduced with bash-4.0-alpha . 使用bash-4.0-alpha引入了
$BASHPID
。 If you run bash --version
you can find out what version of bash(1)
you're using. 如果你运行
bash --version
你可以找到你正在使用的bash(1)
版本。
If you're going to be doing much bash(1)
work, you'll also need the following: 如果你要做很多
bash(1)
工作,你还需要以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.