5

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?

bossylobster
  • 153
  • 1
  • 5

2 Answers2

10

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,485
  • 1
  • 28
  • 44
  • What I really want is to trigger a method when EOF is encountered by the terminal, not execute another method. The snippet I showed above makes it so `script.sh` executes if I manually execute `exit`, but I don't want to do this. – bossylobster Feb 12 '13 at 18:17
  • I don't understand what you want. Can you explain what is, in your opinion, the difference between "trigger a method" and "execute a method"? In your question, you overwrite "exit", thus when you type `exit`, your script is run, but not when you end the shell with Ctrl+D. What I have written here is a way to execute arbitrary commands whenever the shell exits, be it through typing `exit` **or** Ctrl+D. Are you saying that this wasn't your intention? Could you then edit your question as to what you're really looking for? – Stefan Seidel Feb 12 '13 at 22:17
  • It seems I am unclear on what this does. Where do I place this? I've put it in my `.bashrc` and the same behavior is observed on `EOF`. – bossylobster Feb 12 '13 at 23:45
  • I have modified my answer to include some explanation: `trap **** exit` causes this command (any bash command, can be built-in or self-defined function) to be executed whenever the shell exits (no matter if through `EOF` or `exit`). The question for me is still: what do you actually want? Would you want to *only* execute your command on `EOF`, but *not* upon typing `exit`? – Stefan Seidel Feb 13 '13 at 07:36
  • I want to execute it in either situation. It's a clean up for values that get tracked when `.bashrc` runs (when shell opens) and when `cd` is called (I briefly reference `cd` in my original question). – bossylobster Feb 13 '13 at 08:28
  • Then the trap should work, did you try and did it not work? `bash` -> `trap "echo I quit\!" exit` -> `exit` or `EOF` -> the text *I quit!* is printed. – Stefan Seidel Feb 13 '13 at 09:02
  • NOTE: returning weird characters on exit instead of numbers may result in some other scripts failing when they are not supposed to. exit [number] makes the return value available to the parent. The parent calls wait or waitpid to interpret the result of the child process's exit. This interpretation is based on the lowest eight bits of the return value. exit 0 is success, exit non-zero is considered some kind of failure. Or possibly a warning. – jim mcnamara Feb 14 '13 at 01:37
0

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

Slartibartfast
  • 7,978
  • 2
  • 25
  • 27