Bash – How to have the .bashrc only invoke TMUX if it exists on the system

bashrctmux

I use tmux on most boxes, but not all.

I have the following line in my .bashrc file:

[ -z "$TMUX" ] && export TERM=xterm-256color && exec tmux

which invoke tmux if it exists.

I thought the [ -z $TMUX ] meant that it would only be used if tmux exists.

However on another system, without tmux, I get

-bash: exec: tmux: not found

and the login attempt fails

On my main system I see

$ echo $TMUX
/private/var/folders/ks/266xy5lj7x35gfj4csc66444b48rvq/T/tmux-373580663/default,55084,4

I also tried just [ $TMUX ] (i.e. no -z) but that didn't help

I'm also looking for a robust solution that works in Ubuntu as well as OSX

Best Answer

Did you do echo $TMUX, while in a tmux session? Because TMUX is only set, when in a session.

Try that instead:

[ -z "$TMUX" ] && command -v tmux >/dev/null && TERM=xterm-256color exec tmux
Related Question