1

I'm trying to write a bash script to manage setting up a user profile. I can't seem to figure out why this if statement won't work. I have the code:

#!/bin/bash
#check if programs are installed
( TMUX=$(tmux -V) && echo "tmux at version $TMUX" ) || echo "tmux not installed"
if [ $TMUX != "1.8" ]; then
    echo "installing tmux"
fi

but not matter what I try, I constantly get the error

test.sh: line 6: syntax error near unexpected token `fi'
test.sh: line 6: `fi'

Any ideas as to what could cause this?

Jacobm001
  • 151

2 Answers2

5

If you run tmux -v and tmux is not installed you will get an error. Assigning the variable in a subshell would also mean it's never defined outside unless exported. Rather try and check it with which:

TMUX="$(which tmux > /dev/null && tmux -v)"
if [ "$TMUX" != "1.8" ]; then
    echo "installing tmux"
fi

String comparison with test requires you to quote your argument, otherwise you'd get an error about a unary operator expected from Bash.

slhck
  • 228,104
4

From your code, it appears that you set the value of TMUX in a subshell so you wouldn't have that value be defined when you are in the if statement. Since you would be essentially doing an if statement with an unset variable, this will cause a syntax error. I don't see any reason for you to have the subshell in your first line of code. The code will work correctly without it.