Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

share|improve this question
up vote 7 down vote accepted

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
share|improve this answer
A cleaner solution is which tmux &>/dev/null && .... Capturing the output and testing if empty is just dirty :-) – Patrick Jul 23 '14 at 14:40
@polym which(1) is an external program used to search PATH for an executable. It behaves differently on different systems and you can't rely on a useful exit code; use (from most to least portable) ''command -v'' or ''type -P'' (to find the path) or ''hash'' (to check) instead. See mywiki.wooledge.org/BashFAQ/081 – val0x00ff Jul 23 '14 at 14:45
Another nitpick: TERM=xterm-256color exec tmux :-) – Patrick Jul 23 '14 at 14:46
@VolkerSiegel Depends on how this is being implemented. If this is simply being placed at the top of the .bashrc, then you are correct, it's a problem. A complete solution would involve a combination of checking to see if $TMUX is empty, and if the executable is present. – Patrick Jul 23 '14 at 14:48
@mikeserv Thanks, adapted it! :) – polym Jul 23 '14 at 15:20

Ok, so if you want to start tmux only when it exists in $PATH and when it isn't already running then you can do:

command -v tmux >/dev/null && ${TMUX+:} TERM=xterm-256color exec tmux

Shorter still:

tmux -c "${TMUX+!} :" 2>/dev/null && exec tmux -2

The second one asks tmux to return 0 if ${TMUX} is not set && if true, the shell execs tmux -2 to force 256-color. It will only ever return true if tmux can be run in $PATH and if the environment variablete $TMUX does not exist.

share|improve this answer
The "variable is not defined or we comment out the whole command with :" is interesting! – Volker Siegel Jul 23 '14 at 14:51
Thanks for pointing it out! Changed it the comment above. – Volker Siegel Jul 23 '14 at 14:56


which tmux && export TERM=xterm-256color && exec tmux

If there is tmux, which tmux will evaluate to path to tmux binary, therefore to true. If there is no tmux, it will evaluate to empty string, so to false.

share|improve this answer
nah, it doesn't work, see unix.stackexchange.com/questions/146128/… – polym Jul 23 '14 at 15:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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