Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if I'm in a child shell

Tags:

bash

shell

If I'm using bash and type bash I'm in the child shell and need to type exit to go back to the original parent shell. If I forget which one I'm in how do I check?

like image 741
stackjlei Avatar asked Jun 04 '17 07:06

stackjlei


People also ask

What is a child shell?

When you run a program in your shell, a process is created. This new process is called a child process of the shell. The originating process (the shell from which you ran the command) is called the parent process of the child. When you run a new shell, you are creating a child process under the originating shell.

How do you know if a shell is interactive?

If a script needs to test whether it is running in an interactive shell, it is simply a matter of finding whether the prompt variable, $PS1 is set. (If the user is being prompted for input, then the script needs to display a prompt.) Alternatively, the script can test for the presence of option "i" in the $- flag.


1 Answers

Use the SHLVL environment variable.

man bash:

SHLVL : Incremented by one each time an instance of bash is started.

$ echo "$SHLV"
1

$ bash

$ echo "$SHLV"
2

$ exit

$ echo "$SHLV"
1
like image 143
Travis Clarke Avatar answered Sep 23 '22 10:09

Travis Clarke