Você pode usar o comando pstree(que vem por padrão com o Ubuntu). Aqui está um exemplo - atualmente, estou tendo apenas uma janela de terminal aberta na WSL:
User@Wsl:~$ pstree
init─┬─init───bash───pstree
└─{init}
User@Wsl:~$ bash
User@Wsl:~$ sh
$ bash
User@Wsl:~$ pstree
init─┬─init───bash───bash───sh───bash───pstree
└─{init}
Dentro de um ambiente Linux / Ubuntu real, a árvore de processos será mais complicada. Podemos filtrar a árvore pela opção -sque mostrará os pais de um processo selecionado. Portanto, nosso comando poderia ser pstree -s $$, onde $$está uma variável de ambiente que contém o PID atual:
User@Ubuntu:~$ pstree -s $$
systemd──lightdm──lightdm──upstart──gnome-terminal-──bash──pstree
User@Ubuntu:~$ bash
User@Ubuntu:~$ sh
$ bash
User@Ubuntu:~$ pstree -s $$
systemd──lightdm──lightdm──upstart──gnome-terminal-──bash──bash──sh──bash──pstree
Referências:
Adicionar indicador ao prompt do shell: com base na idéia do @ waltinator , para ter um contador na frente do prompt para vários shells diferentes quando o nível for mais profundo do que um, adicionei as linhas, mostradas abaixo da demonstração, na parte inferior dos arquivos de comandos de execução relevantes ( ~/.*rc).
Fiz testes na WSL, Ubuntu 16.04, Ubuntu 18.04 (servidor / desktop), Ubuntu 19.04, na sessão gnome-terminal, tty e ssh. Aqui está como isso funciona:

A limitação é a seguinte: o contador funciona apenas para 13 a 14 níveis de profundidade, dependendo do sistema operacional. Não pretendo investigar os motivos :)
bash> .bashrc:
DEPTH=$(($(pstree -s $$ | sed -r 's/-+/\n/g' | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh)\>') - 1))
if (( DEPTH > 1 )); then PS1=$DEPTH:$PS1; fi
cshe tcsh> .cshrc:
@ DEPTH = `pstree -s $$ | sed -r 's/-+/\n/g' | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh)\>'` - 0
if ( $DEPTH > 1 ) then; set prompt="$DEPTH":"$prompt"; endif
zsh> .zshrc:
DEPTH=$(($(pstree -s $$ | sed -r 's/-+/\n/g' | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh)\>') - 1))
if (( DEPTH > 1 )); then PROMPT=$DEPTH:$PROMPT; fi
ksh> .kshrc:
DEPTH=$(($(pstree -s $$ | sed -r 's/\-+/\n/g' | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh)\>') - 0))
if (( DEPTH > 1 )); then PS1="$DEPTH":"$PS1"'$ '; fi
shque está realmente dashno Ubuntu - aqui as coisas são um pouco complicadas e cabeadas (leia as referências abaixo para obter mais informações):
Edite o ~/.profilearquivo e adicione a seguinte linha na parte inferior:
ENV=$HOME/.shrc; export ENV
Crie o arquivo ~/.shrccom o seguinte conteúdo, observe kshtambém o seguinte $ENV:
#!/bin/dash
DEPTH=$(pstree -s $$ | sed -r 's/-+/\n/g' | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh)\>')
if [ "$0" != 'ksh' ]; then DEPTH=$((DEPTH - 1)); fi
if [ "$DEPTH" -gt 1 ]; then export PS1='$DEPTH:\$ '; fi
Referências:
Crie um comando que produzirá a profundidade: Outra opção é criar um comando shell que produzirá a profundidade. Para esse fim, crie o arquivo executável (portanto, ele deve estar acessível em todo o sistema):/usr/local/bin/depth
sudo touch /usr/local/bin/depth
sudo chmod +x /usr/local/bin/depth
Edite o arquivo com seu editor favorito e adicione as seguintes linhas como conteúdo:
#!/bin/bash
SHELLS='(bash|zsh|sh|dash|ksh|csh|tcsh)'
DEPTH=$(pstree -s $$ | sed -r 's/-+/\n/g' | grep -Ec "\<$SHELLS\>")
if [[ $@ =~ -v ]]
then
pstree -s $$ | sed -r 's/-+/\n/g' | grep -E "\<$SHELLS\>" | cat -n
fi
echo "DEPTH: $DEPTH"
[[ $DEPTH -gt 1 ]] && exit 0 || exit 1
O script acima tem duas opções -vou --verboseque produzirão uma lista dos shells envolvidos. E a outra opção que verificará se a profundidade é maior que uma e com base nisso retornará exit 0ou exit 1, para que você possa usá-la dessa maneira depth && exit. Aqui estão alguns exemplos de uso:
User@Ubuntu:~$ depth # we are at the 1st level - bash
DEPTH: 1
User@Ubuntu:~$ sh
$ csh # we are at the 2nd level - dash
Ubuntu:~% depth # we are at the 3rd level - csh
DEPTH: 3
Ubuntu:~% ksh
$ depth -v # we are at the 4th level - ksh
1 bash
2 sh
3 csh
4 ksh
DEPTH: 4
$ depth && exit # exit to the 3rd level - csh
DEPTH: 4
Ubuntu:~% depth && exit # exit to the 2nd level - dash
DEPTH: 3
exit
$ depth && exit # exit to the 1st level - bash
DEPTH: 2
User@Ubuntu:~$ depth && exit # stay at the 1st level - bash
DEPTH: 1
User@Ubuntu:~$ depth && exit # stay at the 1st level - bash
DEPTH: 1
Comparação por outras soluções: Passei algum tempo adicional para descobrir algumas fraquezas das abordagens fornecidas aqui. Consegui imaginar os dois casos a seguir (as letras maiúsculas são necessárias para melhor destaque da sintaxe):
Quando suou sudo -iestá envolvido:
User@Ubuntu:~$ ps | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh|su|sudo)\>'
1
User@Ubuntu:~$ echo $SHLVL
1
User@Ubuntu:~$ depth
DEPTH: 1
User@Ubuntu:~$ su spas
Password:
Spas@Ubuntu:~$ ps | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh|su|sudo)\>'
1
Spas@Ubuntu:~$ echo $SHLVL
2
Spas@Ubuntu:~$ depth
DEPTH: 2
Spas@Ubuntu:~$ sudo -i
[sudo] password for spas:
Root@Ubuntu:~# ps | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh|su|sudo)\>'
3
Root@Ubuntu:~# echo $SHLVL
1
Root@Ubuntu:~# depth
DEPTH: 3
Quando um processo em segundo plano é iniciado:
User@Ubuntu:~$ bash
User@Ubuntu:~$ ps | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh)\>'
2
User@Ubuntu:~$ echo $SHLVL
2
User@Ubuntu:~$ depth
DEPTH: 2
User@Ubuntu:~$ while true; do sleep 10; done &
[1] 10886
User@Ubuntu:~$ ps | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh)\>'
3
User@Ubuntu:~$ echo $SHLVL
2
User@Ubuntu:~$ depth
DEPTH: 2
# Note: $SHLVL is not supported only by sh/dash.
# It works with all other tested shells: bash, zsh, csh, tcsh, ksh
User@Ubuntu:~$ sh
$ ps | grep -Ec '\<(bash|zsh|sh|dash|ksh|csh|tcsh)\>'
4
$ echo $SHLVL
2
$ depth
DEPTH: 3