Wednesday, January 13, 2010

Prevent exit of shell with Ctrl d - IGNOREEOF

The function of 'Ctrl-d'(^D) key is to exit the shell. If IGNOREEOF variable does not exist in your shell a single ^D will exit from the shell (this is the default behavior).

What is this $IGNOREEOF ?

It controls the action of the shell on receipt of an EOF character as the sole input.

If set, the value is the number of consecutive EOF characters typed as the first characters on an input line before bash exits.

If the variable exists but does not have a numeric value, or has no value, the default value is 10.

Now if you set the following:

export IGNOREEOF=1

This will prevent the shell to exit with a single press of 'Ctrl d' and will require two consecutive 'Ctrl d' (^D) to exit the session. You can also add it to your '~/.bashrc'.

Related bash tips:

- Remove path from PATH variable in bash
- Linux command line history with timestamp
- Bash - save command in history without executing it


ps axu -o user | grep -i root said...

Nice tip, thanks.

Unknown said...

Recently I got a comment like 'why would anyone want this?' . My answer would be: if someone by accident press ^D, at least he/she is not going to loss a shell session which might be useful.

Doug Ballance said...

Thanks for the info. I used this because as a frequent user of pythons interactive shell (which also uses ctrl-d to exit) I was always accidentally closing my terminal sessions when I just wanted to just close the python interpreter.

© Jadu Saikia