Linux 中 source命令 和 bash命令的区别

 

001、

Linuxbash和source命令的区别在于,当你用bash命令执行脚本时,它告诉Linux内核创建一个新的Bash进程来读取和执行脚本,将输出复制到原先的shell进程中,并显示下来.

然而source命令是一个外置的shell,它读取和评估当前shell进程中的文件。为此,脚本所做的所有修改都将保留在Bashshell中

 

a、bash

[root@PC1 test2]# ls
test.txt
[root@PC1 test2]# cat test.txt         ## 准备一个测试脚本
var1=100
echo $var1
[root@PC1 test2]# echo $var1           ## 此时的var1变量为空

[root@PC1 test2]# bash test.txt        ## 利用bash命令执行该测试脚本
100
[root@PC1 test2]# echo $var1           ## var1变量没有任何变化

 

b、source

[root@PC1 test2]# ls
test.txt
[root@PC1 test2]# cat test.txt        
var1=100
echo $var1
[root@PC1 test2]# echo $var1

[root@PC1 test2]# source test.txt           ## 利用source执行该命令
100
[root@PC1 test2]# echo $var1                ## var1变量被修改为100
100

 。

 

002、实例:软件环境变量

[root@PC1 plink]# ls
LICENSE  plink  plink_linux_x86_64_20241022.zip  prettify  test.sh  toy.map  toy.ped
[root@PC1 plink]# pwd
/home/software/plink
[root@PC1 plink]# cat test.sh          ## 测试脚本
PATH=$PATH:/home/software/plink
[root@PC1 plink]# plink                ## 测试软件是否有环境变量
bash: plink: command not found...
Similar command is: 'link'
[root@PC1 plink]# bash test.sh         ## 利用bash执行该脚本
[root@PC1 plink]# plink                ## plink仍然不可调用
bash: plink: command not found...
Similar command is: 'link'
[root@PC1 plink]# source test.sh       ## 利用source执行该脚本
[root@PC1 plink]# plink                ## plink可以调用
PLINK v1.9.0-b.7.7 64-bit (22 Oct 2024)            cog-genomics.org/plink/1.9/
(C) 2005-2024 Shaun Purcell, Christopher Chang   GNU General Public License v3

  plink <input flag(s)...> [command flag(s)...] [other flag(s)...]
  plink --help [flag name(s)...]

Commands include --make-bed, --recode, --flip-scan, --merge-list,
--write-snplist, --list-duplicate-vars, --freqx, --missing, --test-mishap,
--hardy, --mendel, --ibc, --impute-sex, --indep-pairphase, --r2, --show-tags,
--blocks, --distance, --genome, --homozyg, --make-rel, --make-grm-gz,
--rel-cutoff, --cluster, --pca, --neighbour, --ibs-test, --regress-distance,
--model, --bd, --gxe, --logistic, --dosage, --lasso, --test-missing,
--make-perm-pheno, --tdt, --qfam, --annotate, --clump, --gene-report,
--meta-analysis, --epistasis, --fast-epistasis, and --score.

"plink --help | more" describes all functions (warning: long).

 

小结:

01、bash会创建一个子叉shell进行来执行脚本,变量的修改不影响父进程的环境变量

02、source执行脚本则会直接在当前的shell中执行命令,变量的修改直接影响环境变量。

 

posted @ 2025-02-23 18:11  小鲨鱼2018  阅读(65)  评论(0)    收藏  举报