a shell script:
VAR=(aa bb cc)
for i in "${VAR[@]}"
do
echo $i;
done
when run it using . ar_test.sh, it works.
zhangyf@zhangyf-desktop:~/test$ . ar_test.sh
aa
bb
cc
but fails in this way,
zhangyf@zhangyf-desktop:~/test$ ./ar_test.sh
./ar_test.sh: 9: Syntax error: "(" unexpected
There are other lines in the file, so line 9 is actually VAR=(aa bb cc). I know the difference is that the latter forks a new shell process while the former ones run the script in the current shell, but why does the result differs so much?
The difference is not a fork, but different shells.
.sources file in the current shell and./ar_test.shruns executable with default shell (/bin/sh), which may not support arrays. Use shebang as the first line of your script to specify proper shell: