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?

0

2 Answers 2

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.

4
  • Still gives me the same error about the if structure. Commented Sep 11, 2013 at 21:27
  • Not for me. (At least when copypasting your code from here.) Have you made sure there are no bogus characters anywhere? Commented Sep 11, 2013 at 21:29
  • I copy and pasted it exactly as you have it. It's saved in utf-8. Commented Sep 11, 2013 at 21:30
  • See my updated answer Commented Sep 12, 2013 at 7:18
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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.