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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With