How to properly launch tmux on terminal startup?

Solution 1:

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
fi