3

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)

3 Answers 3

4

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

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

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.

EDIT

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

ps -o comm= -p $PPID
5
  • 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, 2011 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, 2011 at 17:34
  • Correct, ps -o comm= -p $PPID also works perfectly.
    – Zabba
    May 2, 2011 at 17:35
  • @bmk: oh yeah, I started with echo $PPID, and modified this without thinking :), -1 for enzotib
    – enzotib
    May 2, 2011 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, 2011 at 17:49
0

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

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

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .