shell 指令碼各種執行方式(source ./*.sh, . ./*.sh, ./*.sh)的區別
阿新 • • 發佈:2019-01-09
原文出處:http://blog.csdn.net/dance_rise/article/details/8573560
結論一: ./*.sh的執行方式等價於sh ./*.sh或者bash ./*.sh,此三種執行指令碼的方式都是重新啟動一個子shell,在子shell中執行此指令碼。
結論二: .source ./*.sh和 . ./*.sh的執行方式是等價的,即兩種執行方式都是在當前shell程序中執行此指令碼,而不是重新啟動一個shell 而在子shell程序中執行此指令碼。
驗證依據:沒有被export匯出的變數(即非環境變數)是不能被子shell繼承的
驗證結果:
[[email protected]~]#name=dangxu //定義一般變數 [[email protected] ~]# echo ${name} dangxu [[email protected] ~]# cat test.sh //驗證指令碼,例項化標題中的./*.sh #!/bin/sh echo ${name} [[email protected] ~]# ls -l test.sh //驗證指令碼可執行 -rwxr-xr-x 1 root root 23 Feb 6 11:09 test.sh [[email protected] ~]# ./test.sh //以下三個命令證明了結論一 [[email protected] ~]# sh ./test.sh [[email protected] ~]# bash ./test.sh [[email protected] ~]# . ./test.sh //以下兩個命令證明了結論二 dangxu [[email protected] ~]# source ./test.sh dangxu [[email protected] ~]#