Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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'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:

#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"

but not matter what I try, I constantly get the error line 6: syntax error near unexpected token `fi' line 6: `fi'

Any ideas as to what could cause this?

share|improve this question

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"

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

share|improve this answer
Still gives me the same error about the if structure. – Jacobm001 Sep 11 '13 at 21:27
Not for me. (At least when copypasting your code from here.) Have you made sure there are no bogus characters anywhere? – slhck Sep 11 '13 at 21:29
I copy and pasted it exactly as you have it. It's saved in utf-8. – Jacobm001 Sep 11 '13 at 21:30
See my updated answer – slhck Sep 12 '13 at 7:18

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .