Strovent SystemsSign in
Reference

Ctrl+D logs you out of the shell: IGNOREEOF and ignoreeof

Ctrl+D at an empty prompt is end of input, so the shell exits and the SSH session or tmux pane closes. Set IGNOREEOF to make Bash ask for exit instead.

Pressing Ctrl+D at an empty shell prompt ends the shell. Over SSH or Mosh that ends the login and closes the connection. In tmux it closes the pane, and when the last pane of the last window goes, the tmux session ends with it.

Why Ctrl+D does this

Ctrl+D is the terminal's end-of-file (EOF) character. stty -a shows it as eof = ^D. A program reading from the terminal sees EOF as "no more input". For an interactive shell at an empty prompt, no more input means it is done, so it exits, the same as typing exit.

When there is text on the line, Ctrl+D does something else: in Bash's line editor it deletes the character under the cursor. Programs such as cat or a python3 prompt also end on Ctrl+D, which is why it is handy and easy to hit by accident.

Make Bash ask for exit: IGNOREEOF

Set the IGNOREEOF variable in ~/.bashrc:

IGNOREEOF=2

Now Bash ignores up to that many Ctrl+D presses in a row on an empty line and prints a reminder each time:

Use "exit" to leave the shell.

(At a login shell it says logout.) Pressing Ctrl+D again after the limit exits. With no number, or a value that is not a number, the limit is 10. This only affects interactive shells.

set -o ignoreeof does the same as IGNOREEOF=10. set +o ignoreeof turns it off.

In zsh the option has the same name: setopt ignoreeof.

Keep tmux panes open

If you use tmux, you can also keep a pane on screen when its shell exits with the window option remain-on-exit:

set-option -g remain-on-exit on

The pane stays open after its shell exits, with a line of text at the bottom, until you close it or start a new shell in it with respawn-pane. So an accidental Ctrl+D does not take the window or the session with it. See Keep your work after a disconnect with tmux, and attach automatically.

Sources

Spotted a mistake? Email [email protected].