23

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?

Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53
captain
  • 1,747
  • 5
  • 20
  • 32

2 Answers2

30

Looking at documentation on 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. 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.
  2. 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.
  3. BASH_SUBSHELL is a variable.
  4. Maybe BASHPID isn't supported by the version of bash you have? I doubt it's a "Mac" problem.
Will Vousden
  • 32,488
  • 9
  • 84
  • 95
imm
  • 5,837
  • 1
  • 26
  • 32
7

It'd be best to get well-acquainted with 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. If you run bash --version you can find out what version of bash(1) you're using.

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

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • So $BASHPID doesn't work for me as I have version 3.2.48. probably I need to update my bash version. – captain Oct 22 '11 at 08:25
  • Do not upgrade the OS X provided `bash`. If you want a new version of `bash(1)` for some reason, be sure to install it in such a way that it does not influence the system-provided shell in any way. I have no idea how much backwards compatibility bash4 has kept with bash3, but unless Apple made some effort to stay within the limits of POSIX `sh` (or some other restricted subset of functionality), upgrading versions might break something. – sarnold Oct 22 '11 at 08:42
  • 8
    If you don't have $BASHPID in your bash version, you can use export BASHPID=$(sh -c 'echo $PPID') to get it – ACyclic Aug 23 '13 at 16:37