0

Is there a simple way to require confirmation before logging out of a shell, and to prevent the shell from exiting if confirmation is denied? This would be useful to avoid accidentally terminating an SSH session used for tunneling.

The problem with trap ... EXIT is it still exits after completing the trap. The most promising solution I found so far is described in confirmed exit using trap, which discusses ctrl+c but seems like it could be made to work for ctrl+d instead.

4
set -o ignoreeof

This will cause the interactive shell to ignore EOF (Ctrl+D).

The bash shell will print

Use "exit" to leave the shell.

if you press Ctrl+D.

You may also set the shell variable IGNOREEOF to some positive integer value. The value determines how many times Ctrl+D has to be pressed until the shell actually exits. The effect of doing set -o ignoreeof in bash is the same setting IGNOREEOF=10 (and the other way around too).

The ignoreeof shell option is a POSIX option that should be available in all POSIX-like shells.

Related:

Not the answer you're looking for? Browse other questions tagged or ask your own question.