61

I am often logged in several SSH sessions at once. To logout from several sessions, I press CTRL+d, until I am back on my local machine.

However, I occasionally press it once too many, and my terminal exits.

Is there a way to make CTRL+d unable to close my terminal ?

I am using terminator as my terminal emulator.

1

3 Answers 3

75

You can also disable eof generally in bash:

set -o ignoreeof

4
  • 2
    But if you do that indiscriminately, it'll apply when logged in over ssh as well. Jun 25, 2014 at 23:17
  • 5
    I'm using terminator 0.97. When I do set -o ignoreeof, it blocks ctrl-d (doesn't exit) 10 times with a message: Use "exit" to leave the shell. On the 11th ctrl-d, it exits. Jan 5, 2017 at 20:33
  • OMG, thank you!! That (accidental terminal cloures) was a huge annoyance for me. Added to ~/.bashrc; works in konsole | xfce4-terminal | ... :-D Apr 19, 2018 at 20:12
  • @Gilles Good input, do you have a solution for that?
    – Gauthier
    Feb 13, 2019 at 12:52
43

You can use the IGNOREEOF environment variable in bash. So export IGNOREEOF=42 and you'll have to press Ctrl+D forty-two times before it actually quits your shell.

POSIX set has an -o ignoreeof setting too.

6
  • 8
    Forty-three times: the first forty-two are ignored, and the last one actually quits. :-)
    – deltab
    Jun 25, 2014 at 15:49
  • I figured Forty-three times would be too redundant. Definately the set -o ignoreeof would be more practical
    – ryekayo
    Jun 25, 2014 at 15:57
  • 3
    But if you do that indiscriminately, it'll apply when logged in over ssh as well. Jun 25, 2014 at 23:17
  • But you have to write the command every time you open a terminal.How do you disable this permanently?
    – MycrofD
    Oct 12, 2014 at 7:05
  • 4
    @MycrofD Add this to your .bashrc or .profile. May 21, 2015 at 14:58
2

The Ctrl-D EOF character is being interpreted by the shell, rather than the terminal emulator specifically. Other answers have covered bash settings, although other shells are different.

For C-shells (e.g. tcsh) you can add it to your tcshrc file:

# Prevent accidental logouts completely
set ignoreeof
# Just prevent the first two, and allow the third
set ignoreeof=3

For fish shell, the Ctrl-D behaviour is controlled by the keybinding. The default setting is delete-or-exit, so you can set the keybinding for \cd to delete-char to only support delete.

More details are in the Fish Github issue (e.g. in versions before 3.0 you need to add the bind to a function called fish_user_key_bindings, after 3.0 you can put it into ~/.config/fish/config.fish) but in summary:

bind \cd delete-char  # Don't exit on accidental Ctrl-D
bind \cd\cd\cd delete-or-exit  # Exit on the third one

Not the answer you're looking for? Browse other questions tagged .