Terminal - how to restart session after inadvertently exiting?
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:
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.
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.