Linux bash pid 和 $$ 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7858191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:48:32  来源:igfitidea点击:

Difference between bash pid and $$

linuxmacosbash

提问by captain

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):

我是一个 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.

So here are my questions:

所以这里是我的问题:

1) What does the first echo print? Is this the pid of the parent bash?

1)第一个回声打印什么?这是父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 $BASHPIDdoesn't return anything, I just get a new line. Is this because I'm running this on a mac and $BASHPIDdoesn't work on a mac?

4) 我在 mac 上做所有的事情,我会在几天后在 Linux 机器上尝试所有这些,但是每当我运行这个脚本时,我$BASHPID没有返回任何东西,我只是得到一个新行。这是因为我在 mac 上运行它并且在 mac 上$BASHPID不起作用?

采纳答案by imm

Looking at documentationon this, it looks like:

查看关于此的文档,它看起来像:

  1. $$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. BASHPIDwill 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.
  2. BASH_SUBSHELLindicates 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.
  3. BASH_SUBSHELLis a variable.
  4. Maybe BASHPIDisn't supported by the version of bash you have? I doubt it's a "Mac" problem.
  1. $$表示运行脚本文件的进程 ID。对于任何给定的脚本,当它运行时,它将只有一个“主”进程 ID。无论您调用多少个子shell,$$都将始终返回与脚本关联的第一个进程 ID。BASHPID将向您显示当前 bash 实例的进程 ID,因此在子 shell 中,它将与可能已调用它的“顶级”bash 不同。
  2. BASH_SUBSHELL表示您所在的“子shell 级别”。如果您不在任何子shell 级别中,则您的级别为零。如果在主程序中启动子shell,则子shell 级别为1。如果在该子shell 中启动子shell,则级别为2,以此类推。
  3. BASH_SUBSHELL是一个变量。
  4. 也许BASHPID您拥有的 bash 版本不支持?我怀疑这是“Mac”问题。

回答by sarnold

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.

$BASHPIDwas introduced with bash-4.0-alpha. If you run bash --versionyou can find out what version of bash(1)you're using.

$BASHPID是通过bash-4.0-alpha引入的。如果您运行,bash --version您可以找出bash(1)您正在使用的版本。

If you're going to be doing much bash(1)work, you'll also need the following:

如果您要做大量bash(1)工作,则还需要以下内容: