shell脚本数组报错Syntax error: "(" unexpected-centos易出现问

按照正常的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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值