0

If I'm in a bash shell, because of how stty is set up, typing Ctrl+D will send an EOF to bash, which will then do pretty much the same thing as the command exit does, and exits the shell.

I'm wondering if there's some way I can temporarily re-map it so that bash does not exit upon recieving an EOF, but instead executes some other command of my choosing.


My motivation is that I'm using Python virtualenvs (through virutalenvwrapper). workon foo will enter the virtualenv, and deactivate will leave the virtualenv.

Sometimes, deactivate can be used for clean-up, but when I Ctrl+D, it skips that step.

If I could make it so that Ctrl+D either (A) ran deactivate and just exited the virtualenv (and re-mapped Ctrl+D to exit again) so I could continue my shell, or (B) ran deactivate and then exit so it would still exit the shell but the clean-up would be performed, that would be great.

  • See unix.stackexchange.com/questions/27588 for how to let the shell ignore EOF. – Bodo Feb 21 at 9:22
  • @Bodo That will let me stop Ctrl+D from exiting the shell, but it won't really help me re-map it to something else. – nog642 Feb 21 at 9:25
1

You could trap EXIT to run a specific command/function when the shell exits.

trap deactivate EXIT

will run deactivate just before exiting the shell.

See http://redsymbol.net/articles/bash-exit-traps/ for more examples.

  • Thank you; this worked. I added trap deactivate EXIT to the postactivate file for the environment, and added trap - EXIT to the postdeactivate file. This accomplishes (B) from the question. – nog642 Feb 21 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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