2009-05-25 54 views
22

出於某種原因,我需要我的腳本能夠接受帶空格字符的參數。舉例來說,如果我有一個腳本如下,在shell腳本中傳遞包含空間的參數

for SOME_VAR in [email protected] 
do 
    echo "$SOME_VAR" 
    cd "$SOME_VAR" 
done; 

,如果我將參數傳遞給腳本(假定它稱爲foo.sh)

sh foo.sh "Hello world" 

我期待的劇本打印世界,你好並將目錄更改爲「Hello world」。但我得到這個錯誤信息,而不是

hello 
cd: 5: can't cd to hello 
world 
cd: 5: can't cd to world 

那麼我如何將參數與空間字符傳遞給shell腳本中的命令?

回答

40

,必須用引號[email protected],太:"[email protected]"

這告訴shell忽略的參數空間;它不會將所有參數變成一個很長的字符串。

+0

XD感謝您的回覆,我就像要自己回答這個問題,進一步的解釋http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST – Jeffrey04 2009-05-25 09:05:18

相關問題