Linux 中 source 命令

发布时间 2023-11-12 17:47:01作者: 小鲨鱼2018

 

source 命令的作用:

a、刷新环境变量

b、执行shell脚本

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

d、从其他函数中读取环境变量

 

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、