原文地址:http://blog.csdn.net/onlyqi/article/details/7191456
關於兩種運行sh的方式: ./myscript.ksh and . ./myscript.ksh
這個問題困擾我很久很久了。。。有時候用這個就行,有時候用那個才行,到底有什麼區別?
Difference Between executing like ./myscript.ksh and . ./myscript.ksh (兩個點之間有空格)
我參考了下面這兩篇帖子:
http://www.unix.com/unix-dummies-questions-answers/156460-difference-between-executing-llike-myscript-ksh-myscript-ksh.html
http://publib.boulder.ibm.com/infocenter/zvm/v5r4/index.jsp?topic=/com.ibm.zvm.v54.dmsp3/comd672.htm
. myscript.ksh的意思是,Run a shell file in the current environment。
也就是說executes ksh in current shell. Since it's run in the current shell, any variables it sets, for example, are available in your current shell.
但我們常用的形式是. ./myscript.ksh (中間有空格). 這是因爲文件名的緣故:
If there are slashes in the file name, . (dot) looks for the named file. If there are no slashes . (dot) uses the search PATH variableto find file.
This may surprise some people when they use dot torun a file in the working directory, but their search rules are not set upto look at the working directory. As a result, the shell doesn't find theshell file. If you have this problem, you can use:
. ./myscript.ksh
因此上句的意思就是,使用current environment運行這個腳本,同時,在工作目錄搜索該腳本。
所以現在很清楚了, ./myscript.ksh的意思是執行當前工作目錄中的這個腳本,但是是在不同的shell中。
二者的不同會導致一個明顯的區別:
如果在一個ksh中設置了一些變量,然後調用了:
ksh `. ./myscript.ksh` 那麼之前設置的變量可以在子腳本,即myscript.ksh中使用。
如果調用的是:
ksh `./myscript.ksh` 那麼之前設置的變量不能在myscript.ksh中使用。
注意:(1) .[空格]./temp/test.ksh 執行當前目錄temp目錄下的test.ksh文件 或者.[空格]temp/test.ksh
.[空格]/temp/test.ksh 只有一個點,執行系統目錄/temp下的test.ksh文件