Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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)

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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
share|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
add comment

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

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

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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