简体   繁体   English

bash pid和$$之间的区别

[英]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: 看一下这方面的文档 ,它看起来像:

  1. $$ 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. 对于任何给定的脚本,当它运行时,它只有一个“主”进程ID。 Regardless of how many subshells you invoke, $$ will always return the first process ID associated with the script. 无论您调用多少个子shell, $$都将返回与该脚本关联的第一个进程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不同。
  2. 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. 如果在主程序中启动子shell,则子shell级别为1.如果在该子shell中启动子shell,则级别为2,依此类推。
  3. BASH_SUBSHELL is a variable. BASH_SUBSHELL是一个变量。
  4. Maybe BASHPID isn't supported by the version of bash you have? 也许你的bash版本不支持BASHPID I doubt it's a "Mac" problem. 我怀疑这是一个“Mac”问题。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM