请问下面这个shell脚本有什么问题?为什么函数执行不了?

1 #!/bin/sh 2 #function 3 if [ -d /etc ] 4 then 5 fun 6 fi 7 8 fun(){ 9 /bin/echo "hello" 10 }执行结果如下:[root@localhost shellScript]# sh function.sh function.sh: line 5: fun: command not found[root@localhost shellScript]# vi function.sh fun是个函数,在下面定义了,为什么还提示command not found?
2026年09月25日 18:37
有1个网友回答
网友(1):

#!/bin/sh
#function
fun(){
/bin/echo "hello"
}
if [ -d /etc ]
then
fun
fi


必须在调用一个函数之前先对他进行定义