linux中创建自定义函数、加载自定义函数、调用自定义函数

 

1、创建自定义函数

格式:

function name {

  command

}

[root@rhel7pc1 test]# ls test.sh [root@rhel7pc1 test]# cat test.sh   ## shell中创建函数格式 #!/bin/bash function fun_test {         echo hello world! }

 

2、加载、调用函数

[root@rhel7pc1 test]# ls test.sh [root@rhel7pc1 test]# cat test.sh #!/bin/bash function fun_test {         echo hello world! } [root@rhel7pc1 test]# fun_test              ## 函数未加载前 bash: fun_test: command not found... [root@rhel7pc1 test]# source test.sh        ## 加载函数 [root@rhel7pc1 test]# fun_test              ## 调用函数 hello world!

 

3、取消函数加载

[root@rhel7pc1 test]# fun_test         ## 调用函数 hello world! [root@rhel7pc1 test]# unset fun_test   ## 取消函数加载 [root@rhel7pc1 test]# fun_test         ## 无法调用函数 bash: fun_test: command not found...