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...