if statement error in bash [duplicate]

2019-01-09 16:34发布

This question already has an answer here:

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.

标签: linux bash
2条回答
疯言疯语
2楼-- · 2019-01-09 16:59

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

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

查看更多
你好瞎i
3楼-- · 2019-01-09 17:18

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

if [ "$i" = "0" ]
查看更多
登录 后发表回答