[linux] Syntax error: “(“ unexpected错误,sh报错

参考:Shell 数组 Syntax error ( unexpected_.sh 使用数组报错-CSDN博客

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

sh test.sh执行脚本的时候,报错:Syntax error: "(" unexpected错误。

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

bash 复制代码
chmod 777 test.sh
#chmod +x test.sh
bash ./test.sh
或者./test.sh

用nohup的时候,记得如果用list的话,只能用bash

bash 复制代码
nohup bash ./shells/multi_task_env.sh >nohup.out 2>&1 &

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

bash 复制代码
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*
相关推荐
Dlrb1211几秒前
Linux系统编程-信号量(线程同步机制)
linux·条件变量·互斥锁·信号量·线程同步
Mr -老鬼2 分钟前
2026移动端自动化平台横向对比指南:15+主流平台深度评测,开发者选型必备
运维·自动化·easyclick·移动测试
无限进步_4 分钟前
Linux进程等待——wait、waitpid与僵尸进程
linux·运维·服务器·开发语言
2401_8346369910 分钟前
Linux集群技术-高可用与负载均衡实战解析
linux·运维·负载均衡
吠品11 分钟前
处理 Python 类继承中那些变来变去的初始化参数
linux·前端·python
会Tk矩阵群控的小木15 分钟前
小红书矩阵软件:基于Python+ADB的多设备批量管理自动化脚本实战
运维·python·adb·矩阵·自动化·新媒体运营·个人开发
NetInside_15 分钟前
某市级水利单位全流量监测与可视化交付实践
运维·网络
ai_coder_ai16 分钟前
使用ocr实现自动化脚本
运维·自动化·ocr
帅大大的架构之路20 分钟前
linux上面的一些小知识点
linux·运维·服务器
光电笑映25 分钟前
进程间通信:深入 System V IPC:共享内存、消息队列与信号量
linux·运维·服务器·c++