I know I'm a few years late to the party, but after reading through @Gilles 's answer, I spent an hour or two redesigning my .profile file to add a signaling mechanism, which I believe might better answer the original question (the one in the body, not the title):
# Start tmux:
if [ -n "$(type tmux 2> /dev/null)" ] && [ -z "$TMUX" ]; then
# Configure a signal handler to allow the tmux session to toggle the exit flag:
export EXIT_FLAG=1
export TMUX_PARENT_PID=$$
trap 'EXIT_FLAG=$(($EXIT_FLAG ^ 1))' USR1
# Start tmux:
tmux new-session -s "shell$$" "TMUX_PARENT_PID=$$ $SHELL -l"
# Exit, assuming the exit flag has not been flipped:
if [ "x$EXIT_FLAG" == "x1" ]; then
exit
fi
# Otherwise, cleanup the environment:
unset EXIT_FLAG
unset TMUX_PARENT_PID
unset TMUX
fi
With this in my .profile, all shells will start tmux by default, and will also exit with tmux. If I want the shell to stick around after I've exited out of tmux, I just have to run kill -s USR1 $TMUX_PARENT_ID before I run exit, and the shell won't exit.
Note that the kill ... command above actually toggles the EXIT_FLAG variable, so running kill ... twice in a row effectively cancels itself out.
<prefix>dto detach the session? Usetmux list-sessionsto go back in tmux.