Linux 中 source 命令

 

source 命令的作用:

a、刷新环境变量

b、执行shell脚本

c、加载函数(环境变量)

d、从另外的shell脚本中读取变量

 

001、 刷新环境变量

(base) [root@pc1 software]# source ~/.bashrc        ## 刷新环境变量
(base) [root@pc1 software]#

 

002、执行shell脚本(和bash的区别)

复制代码
(base) [root@pc1 test02]# cat test.sh
#!/bin/bash

echo $aa
(base) [root@pc1 test02]# aa=100
(base) [root@pc1 test02]# bash test.sh       ## bash不能在子进程中读取外部变量

(base) [root@pc1 test02]# source test.sh     ## 自带 export功能
100
复制代码

 

003、导入函数变量

复制代码
(base) [root@pc1 test02]# ls
fun.sh
(base) [root@pc1 test02]# cat fun.sh         ## 测试函数
#!/bin/bash

function fun_001()
{
        echo "just a test"
}
(base) [root@pc1 test02]# fun_001           ## 直接调用函数,无法调用
bash: fun_001: command not found...
(base) [root@pc1 test02]# source fun.sh     ## source加载函数
(base) [root@pc1 test02]# fun_001           ## 可以直接调用
just a test
复制代码

 

004、

复制代码
(base) [root@pc1 test02]# ls                      ## 两个测试shell脚本
read.sh  var.sh
(base) [root@pc1 test02]# cat var.sh              ## 脚本中定义了三个变量
#!/bin/bash
a=1
b=2
c=3
(base) [root@pc1 test02]# cat read.sh             ## source 从var.sh中读取变量, 然后输出
#!/bin/bash

source ./var.sh
echo $a
echo $b
echo $c
(base) [root@pc1 test02]# bash read.sh            ## 测试效果,说明读取成功
1
2
3
复制代码

 

posted @   小鲨鱼2018  阅读(293)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-11-12 python 中统计fasta文件中每条scaffold中碱基的数目
2022-11-12 linux 中shell 脚本将 gff文件转换为bed文件
2020-11-12 ansible
点击右上角即可分享
微信分享提示