Apple - Terminal - how to restart session after inadvertently exiting?

At this point, there's no way to get the tab back. The terminal session is closed, and it no longer has a TTY. There's just no way to reference the tab in order to do anything clever. I'd suggest adding this function to your .bashrc or .profile so that you won't have the issue in the future:

exit() {
    read -t5 -n1 -p "Do you really wish to exit? [yN] " should_exit || should_exit=y
    case $should_exit in
        [Yy] ) builtin exit $1 ;;
        * ) printf "\n" ;;
    esac
}

or, for those of us who use the Z Shell (add it to your .zshrc):

exit() {
    if read -t5 -q "?Do you really wish to exit? [yN] "; then
        builtin exit $1
    fi
}

It's a nice little barrier between you and that annoying exit command! Lord knows I've done the same thing many times in the past.


William T Froggard's script did not do what I needed, because generally the only way I get into this situation is via ^D (ctrl+D), and redefining exit did nothing for that situation. For me, Dennis Williamson's suggestion of setting IGNOREEOF was enough. I just added:

# Do not exit on a single ^D, require 2 in a row
IGNOREEOF=1

to my ~/.bashrc file and now (if I'm in the top-level shell and would exit the terminal) the first ^D generates the response:

Use "logout" to leave the shell.

If I type ^D again immediately, the shell exits, so exiting when I want is still easy, but now a single ^D will give me a warning. (If you want, you can set IGNOREEOF to a higher number to require additional consecutive ^D's.)

Also helpful, if I'm in a sub-shell, the first ^D generates the response:

Use "exit" to leave the shell.

Again, an extra ^D will get me out, and now I can tell the difference between exiting a sub-shell and exiting the top-level shell.