shell Syntax error: "(" unexpected错误

原文链接:> https://www.zhoubotong.site/post/89.html

执行一个正常的普通do.sh脚本:

#!/bin/bash
# 定义函数
function func() {
n=100
}
# 调用函数
func

echo $n #输出函数内部的变量

使用 ./do.sh和 source do.sh 或者 . do.sh 均能正常执行,问题如下:

uos@uos-PC:~/Desktop$ sh do.sh
do.sh: 3: do.sh: Syntax error: "(" unexpected

什么鬼?经查发现是sh 与 bash 有些地方不兼容导致,直接 bash do.sh 即可。

posted @ 2023-01-03 17:11  周伯通之草堂  阅读(256)  评论(0编辑  收藏  举报