When I am in a terminal, and I type /bin/bash, that starts up another bash shell, right?

Is it possible to find out, via some command, whether the current bash shell has been started from another bash shell?

Also, what are the implications of starting a shell inside another? (or does it not matter)

| improve this question | | | | |

Whit this command you can see the parent process of the current shell:

ps -o comm= -p $(echo $PPID)

This works in bash where PPID variable is defined, don't know in other shells.


As @bmk noticed, the echo stuff is overkill and not necessary:

ps -o comm= -p $PPID
| improve this answer | | | | |
  • Thanks, that works. My results: Running ps -o comm= -p $(echo $PPID) gives gnome-terminal. Running /bin/bash, and then ps -o comm= -p $(echo $PPID) gives bash. – Zabba May 2 '11 at 17:28
  • What do you actually need the echo for? I think it should also work without it: ps -o comm= -p $PPID – bmk May 2 '11 at 17:34
  • Correct, ps -o comm= -p $PPID also works perfectly. – Zabba May 2 '11 at 17:35
  • @bmk: oh yeah, I started with echo $PPID, and modified this without thinking :), -1 for enzotib – enzotib May 2 '11 at 17:35
  • @entotib: There was nothing wrong with it (as the question was tagged with bash). Therefore: It works and it does what it should. It's just a bit overhead and doesn't work with different shells. – bmk May 2 '11 at 17:49

A version that also works with shells other than bash is:

ps -p `ps h -p $$ -o ppid` -o comm=
| improve this answer | | | | |
  • Thanks. (I get the same results as I do for enzotib's answer too) – Zabba May 2 '11 at 17:33

bash actually does count that via a special, exported variable SHLVL.

$ echo "$SHLVL"
$ bash -c 'echo "$SHLVL"'

If SHLVL is 2 though, it doesn't necessarily mean that the parent process is bash, it just means that one of the shell's ancestors is also bash, or another process that has exported SHLVL (ksh sets it too I think).

Why do you need to know if your shell's parent is bash?

And no, there's not really any implications of running a shell inside another shell. It's just like running any other command in a shell.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.