Need a space after [
and no space before or after =
in the assignment. $(($i+1)))
would try to execute the output of the ((...))
expression and I am sure that's not what you want. Also, you are missing a $
before the array name.
With these things corrected, your while loop would be:
#!/bin/bash
i=0
while [ "$i" -le "${#myarray[@]}" ]
do
echo "Welcome $i times"
i=$((i + 1))
done
i=$((i + 1))
can also be written as ((i++))
- it is always better to enclose variables in double quotes inside
[ ... ]
- check your script through shellcheck - you can catch most basic issues there
See also:
- Why should there be a space after '[' and before ']' in Bash?
- How to use double or single brackets, parentheses, curly braces
- Command not found error in Bash variable assignment
- Using [ ] vs [[ ]] in a Bash if statement