Ctrl D not exiting bash after sourcing bashrc

bashbashrc

The bashrc was not written by me, and sources a lot of different configuration files. Upon sourcing it, I can't exit bash with Ctrl+D anymore.

I found this related question about zsh, and I would like to know the mechanism used to do the same with bash.

When typing Ctrl+D in a login shell, I get:

Use "logout" to leave the shell.

When in a non-login shell,

Use "exit" to leave the shell.

As these messages are translated depending on the locale in use, this is probably not a system script. I would like to control both independently, if possible (disable this functionality for non-login shells, for instance). Is that possible?

Best Answer

Have a look at your configuration if a either bash variables or shell options are set:

Bash Variables

(Section 5.2 in the Bash Reference Manual)

IGNOREEOF

Controls the action of the shell on receipt of an EOF character as the sole input. If set, the value denotes the number of consecutive EOF characters that can be read as the first character on an input line before the shell will exit. If the variable exists but does not have a numeric value (or has no value) then the default is 10. If the variable does not exist, then EOF signifies the end of input to the shell. This is only in effect for interactive shells.

For example IGNOREEOF=2


Modifying Shell Behavior: The Set builtin

set allows you to change the values of shell options

(Section 4.3.1 in the Bash Reference Manual)

-o ignoreeof

An interactive shell will not exit upon reading EOF.


Getting to your question

I would like to control both independently, if possible (disable this functionality for non-login shells, for instance). Is that possible?

Yes. Scripts you source should check if the shell is a login shell and set the variable accordingly, something like this:

if shopt -q login_shell; then IGNOREEOF=10; else IGNOREEOF=0; fi

Note the value of 10 still allows you to leave the shell with Ctrl+D; you just need 11 consecutive strokes of it.