I've used this snippet to launch tmux when the terminal is launched:

# TMUX startup
if command -v tmux>/dev/null; then
 [[ ! $TERM =~ screen ]] && [ -z $TMUX ] && exec tmux

But with this I can't exit tmux without the terminal screen being closed too.

I've tried:

Ctrl + b :detach


And looking for the PID and killing it. All those methods close the terminal too.

How should I configure tmux to start when launching the terminal but still being able to close it without the terminal closing? Any tips are appreciated!

  • I've just tried and replacing the snipped I used with just tmux works, but I get a: sessions should be nested with care, unset $TMUX to force. I've also tried leaving the snipped I used as it is and adding a tmux at the end of the ~/.bashrc but then detaching still exits the terminal. – bpinaya Feb 19 at 10:37

The problem is the exec command. As explained here, exec will replace the current shell with whatever you tell it execute. So you don't have a shell that is running tmux, you just have tmux and therefore exiting it will also exit the terminal.

Just remove the exec and it should work as expected:

if command -v tmux>/dev/null; then
 [[ ! $TERM =~ screen ]] && [ -z $TMUX ] && tmux

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.