6

Similar to a post about cd, I've overwritten exit to do the following:

function exit() { $HOME/script.sh && builtin exit "$@"; }

Though this works as expected when exit is called directly, when the shell is exited via CTRL-D, this does not execute, OR, if it does execute, the script doesn't manage to finish.

What gives? Is some method other than exit called when CTRL-D is used?

2 Answers2

11

When you press Ctrl+D, what you really say to bash is EOF (end of file). Thus, the shell just terminates because there's no more input to read. If you want to perform actions on exit, use a trap:

trap "~/script.sh" exit

Using the "exit" trap, you can execute any shell commands just before the shell exits, and it doesn't matter whether the shell was terminated by exit or by Ctrl+D.

Stefan Seidel
  • 10,855
0

From the bash man page: "When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists."