Automatically Start Tmux When Starting Shell In Bash

Add the following to your .bashrc file.

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

This will:

  • Check that tmux exists on the sytem.
  • That an interactive shell is present.
  • That we aren't already running tmux, to prevent it from running inside itself.

Add new comment

The content of this field is kept private and will not be shown publicly.