Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • Do you mean I should always use $BASHPID in place of $$ in any case in bash? When shall I use which? Commented Nov 27, 2018 at 14:09
  • @Tim It depends on whether you, in a subshell, wants to get the process ID of the script or of the subshell. Both possibilities are provided and which is the correct one is dependent on the application. No more specific answer can be given to that. Commented Nov 27, 2018 at 14:20
  • 1
    If I want to get the parent pid of a subshell, that is, the pid of the invoking shell of the subshell, do I have to use $$? Can I use something else which is more predictable? Commented Nov 27, 2018 at 14:26
  • 2
    @Tim The PID of a parent shell of a subshell can't reliably be found unless you arrange to save $BASHPID in a variable and use that in the subshell. There is $PPID, but that's the parent PID of the shell in the same sense that $$ is the PID of the shell (it's not reset in a subshell). There is no $BASHPPID variable. Commented Nov 27, 2018 at 14:57
  • 1
    @FranklinYu BASHPID was introduced in release 4.0. You are either unknowingly running some other shell, or an older version of bash. Commented Sep 29, 2022 at 15:09