exec和source都屬於bash內部命令(builtins commands),在bash下輸入man exec或man source能夠查看全部的內部命令信息。node
bash shell的命令分爲兩類:外部命令和內部命令。外部命令是經過系統調用或獨立的程序實現的,如sed、awk等等。內部命令是由特殊的文件格式(.def)所實現,如cd、history、exec等等。linux
在說明exec和source的區別以前,先說明一下fork的概念。shell
fork是linux的系統調用,用來建立子進程(child process)。子進程是父進程(parent process)的一個副本,從父進程那裏得到必定的資源分配以及繼承父進程的環境。子進程與父進程惟一不一樣的地方在於pid(process id)。bash
環境變量(傳給子進程的變量,遺傳性是本地變量和環境變量的根本區別)只能單向從父進程傳給子進程。無論子進程的環境變量如何變化,都不會影響父進程的環境變量。測試
shell script:ui
有兩種方法執行shell scripts,一種是新產生一個shell,而後執行相應的shell scripts;一種是在當前shell下執行,再也不啓用其餘shell。
新產生一個shell而後再執行scripts的方法是在scripts文件開頭加入如下語句
#!/bin/shspa
通常的script文件(.sh)便是這種用法。這種方法先啓用新的sub-shell(新的子進程),而後在其下執行命令。
另一種方法就是上面說過的source命令,再也不產生新的shell,而在當前shell下執行一切命令。繼承
source:進程
source命令即點(.)命令。ip
在bash下輸入man source,找到source命令解釋處,能夠看到解釋"Read and execute commands from filename in the current shell environment and ..."。從中能夠知道,source命令是在當前進程中執行參數文件中的各個命令,而不是另起子進程(或sub-shell)。source filename or .filename 執行filename中的命令。
exec:
在bash下輸入man exec,找到exec命令解釋處,能夠看到有"No new process is created."這樣的解釋,這就是說exec命令不產生新的子進程。那麼exec與source的區別是什麼呢?
exec命令在執行時會把當前的shell process關閉,而後換到後面的命令繼續執行。
======================================================================================================================
下面咱們寫個腳原本測試一下,這樣你就會很容易的讀懂我上面所說的東西~
1.sh
#!/bin/bash
A=B
echo "PID for 1.sh before exec/source/fork:$$"
export A
echo "1.sh: \$A is $A"
case $1 in
exec)
echo "using exec..."
exec ./2.sh ;;
source)
echo "using source..."
. ./2.sh ;;
*)
echo "using fork by default..."
./2.sh ;;
esac
echo "PID for 1.sh after exec/source/fork:$$"
echo "1.sh: \$A is $A"
2.sh
#!/bin/bash
echo "PID for 2.sh: $$"
echo "2.sh get \$A=$A from 1.sh"
A=C
export A
echo "2.sh: \$A is $A"
=================》》》》》》》》》》》
測試結果:
[root@node2 ~]$ ./1.sh fork
PID for 1.sh before exec/source/fork:10175
1.sh: $A is B
using fork by default...
PID for 2.sh: 10176
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:10175
1.sh: $A is B
=============================================
[root@node2 ~]$ ./1.sh source
PID for 1.sh before exec/source/fork:10185
1.sh: $A is B
using source...
PID for 2.sh: 10185
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:10185
1.sh: $A is C
=============================================
[root@node2 ~]$ ./1.sh exec
PID for 1.sh before exec/source/fork:10194
1.sh: $A is B
using exec...
PID for 2.sh: 10194
2.sh get $A=B from 1.sh
2.sh: $A is C
[cpsuser@cps-svr-153 zy]$
=============================================
從以上結果能夠看出:
1.執行source和exec的過程當中沒有產生新的進程,而fork是默認的運行方式,在運行的過程當中會產生新的進程,也就是子進程
2.source和exec的區別在於exec執行完畢後沒有輸出進程,也就是說運行完畢2.sh後直接退出了,沒有返回1.sh
3.fork和source的最後一句輸出分別爲:1.sh: $A is B (fork,說明它運行的環境不同,要否則輸出的應該是C)
1.sh: $A is C(source,說明從始至終都是在一個shell中執行)
小節: