Shell 数组 Syntax error ( unexpected

test.sh

#!/bin/bash
arr=('a')
echo ${arr[0]}

用下面执行该脚本的时候,报了Syntax error: "(" unexpected错误

sh test.sh

而使用下面这种方式执行,则不会报错

chmod +x test.sh
./test.sh

这事实上与sh执行的shell版本有关系。通过ll /bin/*sh 可以看到 执行sh的时候实际上是执行dash。

ll /bin/*sh
-rwxr-xr-x 1 root root 1113504 May  3  2019 /bin/bash*
-rwxr-xr-x 1 root root  121432 Jan 25  2018 /bin/dash*
lrwxrwxrwx 1 root root       4 May  3  2019 /bin/rbash -> bash*
lrwxrwxrwx 1 root root       4 Jun 24  2019 /bin/sh -> dash*
lrwxrwxrwx 1 root root       7 Mar  7  2019 /bin/static-sh -> busybox*

猜你喜欢

转载自blog.csdn.net/qq_43058685/article/details/105572353
今日推荐