Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

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

Your Answer

 
discard

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.