4
#! /bin/bash

count=1
step=(a b)

for x in 0 1
do
    if [[ $count != '0' ]]; then
        if [[ ${step[x]} = "a"]]; then
            echo "Python test ($count)"
        else
            echo "stress test"
        fi
    fi
done

I get the following error

syntax error in conditional expression: unexpected token `;'
line 20: syntax error near `;
line 20: `        if [[ ${step[x]} = "a"]]; then'

Why?

1
  • 1
    How does line 9 get to be line 20? Commented Feb 2, 2011 at 22:15

2 Answers 2

5

You need a space between "a" and the ]] in the second if.

More technically, [[ and ]] have to be separate tokens, and bash's parser doesn't separate tokens on quotes or most punctuation.

I believe your original code for the line is equivalent to if [[ ${step[x]} = "a]]"; then if that makes the issue more obvious.

Sign up to request clarification or add additional context in comments.

Comments

1

Change

if [[ ${step[x]} = "a"]]; then

to

if [[ ${step[$x]} = "a" ]]; then

i.e. use $x instead of x and add some space after the last parameter.

Update: You don't need to precede variables with a $ sign in array subscripts (also within double parenthesis (( )) or after the let keyword).

4 Comments

Yep, the x in array access is ok, but why?
yeah it worked for me without the $, the issue was that i did not have a space after "a" ] .
yeah it worked for me without the $, the issue was that i did not have a space after "a" ] . thanks
@Slomojo: Array indices are an arithmetic context which doesn't require dollar signs. Similarly: ((a++)) and echo $((a * b))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.