7

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

3 Answers3

7

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
polym
  • 10,852
  • 2
    A cleaner solution is which tmux &>/dev/null && .... Capturing the output and testing if empty is just dirty :-) – phemmer Jul 23 '14 at 14:40
  • @Patrick Thanks, I adapted it to your comment ;) – polym Jul 23 '14 at 14:44
  • 5
    @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 http://mywiki.wooledge.org/BashFAQ/081 – Valentin Bajrami Jul 23 '14 at 14:45
  • 1
    Another nitpick: TERM=xterm-256color exec tmux :-) – phemmer Jul 23 '14 at 14:46
  • 1
    @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. – phemmer Jul 23 '14 at 14:48
  • Yeah, it was better the first way. I upvoted it. – mikeserv Jul 23 '14 at 14:55
  • @val0x00ff , VolkerSiegel, patrick : how about now? – polym Jul 23 '14 at 14:56
  • Better! Warning comment removed. – Volker Siegel Jul 23 '14 at 14:59
  • @VolkerSiegel It's a legit warning :). Haven't thought about that at first, so it's great advice! – polym Jul 23 '14 at 15:00
  • @Gilles That comment wasn't so much about using which as it was about not capturing the output (see original version of the answer). Yes, command -v is safer, but I wasn't addressing that bit :-) – phemmer Jul 23 '14 at 23:10
5

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.

mikeserv
  • 58,310
-1

Use

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.

  • nah, it doesn't work, see http://unix.stackexchange.com/questions/146128/how-can-i-have-my-bashrc-only-invoke-tmux-if-it-exists-on-my-system/146130?noredirect=1#comment-236122 – polym Jul 23 '14 at 15:24