1. 程式人生 > >shell指令碼陣列報錯Syntax error: "(" unexpected

shell指令碼陣列報錯Syntax error: "(" unexpected

按照正常的shell陣列定義
#!/bin/sh
a=(1 2 3)
for number in a[@]doechonumber
done
執行該指令碼,在有的機器上會報錯Syntax error: “(” unexpected

這與你實際使用的shell版本有關。你可以用 ls -l /bin/*sh 打印出來,例如:
-rwxr-xr-x 1 root root 959120 Mar 29 2013 /bin/bash
lrwxrwxrwx 1 root root 21 Nov 22 2013 /bin/csh -> /etc/alternatives/csh
-rwxr-xr-x 1 root root 109768 Mar 30 2012 /bin/dash
lrwxrwxrwx 1 root root 4 Mar 29 2013 /bin/rbash -> bash
lrwxrwxrwx 1 root root 4 Mar 30 2012 /bin/sh -> dash
lrwxrwxrwx 1 root root 7 Nov 17 2012 /bin/static-sh -> busybox
lrwxrwxrwx 1 root root 13 Oct 15 2011 /bin/tcsh -> /usr/bin/tcsh
在這裡,sh被重定向到dash,因此,如果執行./example.sh,則使用的是dash
避免報錯可有多種方法,例如執行 bash example.sh,或者,將指令碼第一行改為
#!/bin/bash,執行./example.sh也可以。