按照正常的shell数组定义,例如3.sh
cat 3.sh
#!/bin/sh
a=( 1 2 3)
for number in ${a[@]}
do
echo $number
done
执行命令
sh 3.sh
3.sh: 2: 3.sh: Syntax error: "(" unexpected
执行该脚本,在有的机器上会报错Syntax error: "(" unexpected
bash 3.sh
1
2
3
与你实际使用的shell版本有关。你可以用 ls -l /bin/*sh 打印出来,例如:
ls -l /bin/*sh
-rwxr-xr-x 1 root root 1021112 Oct 7 2014 /bin/bash
-rwxr-xr-x 1 root root 121272 Feb 19 2014 /bin/dash
lrwxrwxrwx 1 root root 4 Dec 4 01:40 /bin/rbash -> bash
lrwxrwxrwx 1 root root 4 Dec 4 01:40 /bin/sh -> dash
lrwxrwxrwx 1 root root 7 Nov 14 2013 /bin/static-sh -> busybox
在这里,sh被重定向到dash,因此,如果执行./3.sh,则使用的是dash
避免报错可有多种方法,例如执行 bash example.sh,或者,将脚本第一行改为
#!/bin/bash,执行./3.sh也可以。
转载于:https://blog.51cto.com/sry2004/2057190