Shell允许将一组命令集或语句形成一个可用块,这些块成为shell函数。
函数名 ()
{
...
}
function 函数名 ()
{
...
}
函数可以放在同一个文件中作为一段代码,也可以作为一个单独的文件。
#!/bin/bash
#hellofun.sh
function hello(){
echo "Hello,$1 ,tody is `date`"
return 1
}
echo "now going to the function hello"
hello domingo
echo "back from the function"
#!/bin/bash
#func.sh
. hellofun.sh
set
hello root
echo $?
unset hello
hello root
查看和删除函数
set
unset
$?是函数返回值