4
i=0
for f in `awk '{print $1}' config.list`
do
    echo "i value is $i"
    if ["$i" = "0"]
    then
        echo "here"
        i=$((i+1))
        continue 
    fi
    arr[i]=$f  
    i=$((i+1))
done

In the above bash script I am getting an error where i have used the if statement it looks like this

./script.sh: line 5: [0: command not found

Kindly point me out what could be my mistake.

user3840170
  • 26,597
  • 4
  • 30
  • 62
monucool
  • 425
  • 1
  • 6
  • 15
  • 2
    @jedwards, a single equal sign is correct for string equality comparison. With `[[` the double equal sign is for pattern matching. You may be thinking of another language. – glenn jackman Jun 09 '12 at 09:59
  • @glennjackman, [I realize `=` and `==` are synonymous](http://tldp.org/LDP/abs/html/comparison-ops.html), my statement was more for readability and maintainability. Traditionally `=` is for assignment and should be left that way whenever possible (e.g. this case). – jedwards Jun 09 '12 at 17:09

2 Answers2

8

Use if [ "$i" = "0" ]

In bash, you need spaces around [ and ] in if conditions

Hari Menon
  • 33,649
  • 14
  • 85
  • 108
4

You are getting this error, because Bash if statements require the addition of spaces around the operands:

if [ "$i" = "0" ]
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
codaddict
  • 445,704
  • 82
  • 492
  • 529