How often can I type exit without logging out?

You can use the SHLVL variable to determine how far nested in you're, to a shell started by a login process:

$ echo $SHLVL 
1
$ bash
$ echo $SHLVL 
2
$ bash
$ echo $SHLVL 
3
$ sudo su -            # Start a login shell, clears $SHLVL
# echo $SHLVL 
1
# logout
$ sudo su
# echo $SHLVL 
4
# bash
# echo $SHLVL 
5

Since the login shell from su - clears SHLVL, it has SHLVL=1. To quit the nearest such login shell in the shell ancestry, you have to use exit $SHLVL times.


SHLVL is not supported by dash, so whenever it enters the picture, the figure will be wrong. However, dash isn't the login shell for any usable account on Ubuntu, and SHLVL works on more advanced shells like bash and zsh.


I cannot reproduce your missing sudo su:

$ pstree -ps $$
init(1)───sshd(1404)───sshd(12614)───sshd(12673)───zsh(12674)───sudo(31012)───su(31014)───bash(31016)───pstree(31084)

You can simply use the command ps with no arguments. Here I have 3 nested bashes so 3 exits to type:

~ $ ps
  PID TTY          TIME CMD
 1986 pts/2    00:00:10 bash
31351 pts/2    00:00:00 bash
31399 pts/2    00:00:00 bash
31450 pts/2    00:00:00 ps