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

 

posted @   小鲨鱼2018  阅读(1111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-04-16 R语言中fig函数,图形布局的精细控制
2021-04-16 R语言中layout图形布局函数
2021-04-16 R语言中text函数
2021-04-16 R语言中使用locator(1)函数获取图形的位置坐标
2021-04-16 R语言中给图形添加图例
2021-04-16 R语言绘图abline函数 给图形增加参考线
2021-04-16 R语言绘图添加次要刻度线
点击右上角即可分享
微信分享提示