MacOS – Terminal – how to restart session after inadvertently exiting

bashmacosterminal

Sometimes I inadvertently exit from a Terminal session (usually because I think I'm connected to a remote system, when I'm not), so I get to this point:

enter image description here

How I can I restart the session at this point ? I don't want to close the window or tab because I have a bunch of tabs all set up for my normal workflow, so I just want to get the session in the current tab going again (i.e. get back to a bash prompt).

The only solution I've found so far is to quit Terminal completely and open it again, but that is far from ideal as it obviously interrupts anything else I'm doing in other Terminal windows/tabs.

Best Answer

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.