this one is rather complicated to explain and I failed in imagining a short description of this to be able to search for.

My current setting is that when I open a terminal, due to this line

if [[ -n "$PS1" ]] && [[ -z "$TMUX" ]]
        tmux && exit

tmux is started instantly. And since I've got an && exit after the tmux command the the tmux encapsulating shell is closed instantly when exiting tmux.

I think this is a nice behaviour by default, but in rare occasions I want to detach or exit tmux without exiting the encapsulating shell.

I thought I'd can do this by entering exit 1 inside tmux so that the exit code of tmux is !=0 and thus the exit of the encapsulating shell isn't executed, but it seems that tmux is exiting with exit code 0 despite the exit 1 inside.

Can anyone imagine a way to realize my aim to exit the encapsulating bash by default but being able to manually change this?

  • 2
    tmux ; [ -e ~/DoNotExit ] || exit and then create the file if you don't want to exit? Expand the idea to imclude a PID if you have multiple parallel sessions, maybe throw in an automatic rm of the file?
    – icarus
    May 12, 2020 at 11:57
  • Thanks for replying. This is a nice workaround, but regarding the answer I got I think it's better and more convenient to make this without a file to communicate whether the encapsulating shell should terminate or not. But thanks anyway ;)
    – atticus
    May 12, 2020 at 17:22
  • 1
    I agree the answer you got is better and I upvoted it.
    – icarus
    May 13, 2020 at 0:07

1 Answer 1


You can make tmux detach with a nonzero exit status by doing:

tmux detach -E false

Perhaps make an alias to do that.

If you want your shell to exit as well, you can do:

tmux detach -E false; exit
  • Oh thanks that's a nice solution (I think I'll bind the normal detatch to prefix+d and the detatch but don't kill the encapsulating shell to prefix + shift +d)
    – atticus
    May 12, 2020 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.