1

I spent a lot of time to figure out what is the reason I get error in the following simple code. I appreciate if anyone can solve it.

i=0
while read line
do
    if [[ -z "$line" ]]; then
        echo "End of numbers"
        break
    else
    {
        echo "$line is not empty"
        array[$i] = $line
        echo array[$i]
        ((i += 1))  

    }
    fi
done

output:

sss
sss is not empty
command.sh: line 10: array[0]: command not found
array[0]
ss2
ss2 is not empty
command.sh: line 10: array[1]: command not found
array[1]

1 Answer 1

4

Instead of:

array[$i] = $line

You need to remove spaces around = in BASH:

array[$i]="$line"

Or better use this syntax to append an element in array:

array+=( "$line" )
Sign up to request clarification or add additional context in comments.

Comments

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.