shell脚本数组报错Syntax error: "(" unexpected

前端之家收集整理的这篇文章主要介绍了shell脚本数组报错Syntax error: "(" unexpected前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

按照正常的shell数组定义,例如3.sh

  1. cat 3.sh
  2. #!/bin/sh
  3. a=( 1 2 3)
  4. for number in ${a[@]}
  5. do
  6. echo $number
  7. done
  8.  
  9. 执行命令
  10. sh 3.sh
  11. 3.sh: 2: 3.sh: Syntax error: "(" unexpected
  12. 执行该脚本,在有的机器上会报错Syntax error: "(" unexpected
  13.  
  14. bash 3.sh
  15. 1
  16. 2
  17. 3
  18. 与你实际使用的shell版本有关。你可以用 ls -l /bin/*sh 打印出来,例如:
  19. ls -l /bin/*sh
  20. -rwxr-xr-x 1 root root 1021112 Oct 7 2014 /bin/bash
  21. -rwxr-xr-x 1 root root 121272 Feb 19 2014 /bin/dash
  22. lrwxrwxrwx 1 root root 4 Dec 4 01:40 /bin/rbash -> bash
  23. lrwxrwxrwx 1 root root 4 Dec 4 01:40 /bin/sh -> dash
  24. lrwxrwxrwx 1 root root 7 Nov 14 2013 /bin/static-sh -> busyBox

在这里,sh被重定向到dash,因此,如果执行./3.sh,则使用的是dash 避免报错可有多种方法,例如执行 bash example.sh,或者,将脚本第一行改为 #!/bin/bash,执行./3.sh也可以。

猜你在找的Bash相关文章