I'm trying this in 16.04 but it is not working. Taken from: https://stackoverflow.com/a/8880633/2771043.
Edit: Apparently this works if you copy and past it to a terminal but not on a script file. How do I get it to work the same way on a .sh file?
declare -a arr=("element1" "element2" "element3")
for i in "${arr[@]}"
do
echo "$i"
done
I get
old.sh: 2: old.sh: Syntax error: "(" unexpected
If I take out the brackets, I get
old.sh: 2: old.sh: declare: not found
old.sh: 5: old.sh: Bad substitution
It works if you add the crunchbang line at the head end of the script to force the shell interpreter to use the
bash
syntax, and not default to the oldsh
syntax. (It works for me also without the crunchbang line, but for many reasons it is a good idea to use a crunchbang line.)I created the file
arraytest
.Then I made it executable and executed it.