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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM