bashshell

How can I tell if I'm in a child 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?


Solution

  • Use the SHLVL environment variable.

    man bash:

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

    $ echo "$SHLVL"
    1
    
    $ bash
    
    $ echo "$SHLVL"
    2
    
    $ exit
    
    $ echo "$SHLVL"
    1
    

    If you’re using a multiplexer such as tmux, SHLVL also increments by 1 when you attach to a session.