[root@localhost ~]# cat def.sh
#!/bin/bash
#定义函数
hello(){
echo "hello world"
}
echo "start running hello define"
hello #调用函数
[root@localhost ~]# sh def.sh
start running hello define
hello world
#函数调用1,使用source生效函数
[root@localhost ~]# source def.sh
start running hello define
hello world
#函数调用2
[root@localhost ~]# cat def2.sh
#!/bin/bash
source ./def.sh
echo "this is def2.sh"
[root@localhost ~]# sh def2.sh
start running hello define
hello world
this is def2.sh
#函数带入参数
[root@localhost ~]# cat def.sh
#!/bin/bash
hello(){
echo "hello world $1"
}
echo "start running hello define"
hello lisi
hello zhangsan
[root@localhost ~]# sh def.sh
start running hello define
hello world lisi
hello world zhangsan
#return返回值
[root@localhost ~]# cat def.sh
#!/bin/bash
hello(){
echo "hello world $1"
return 222
}
echo "start running hello define"
hello lisi
hello zhangsan
[root@localhost ~]# echo $?
222
函数的加载:source
删除:unset -f DefName