首頁  >  文章  >  php教程  >  Shell 程式設計:Bash空格的那點事

Shell 程式設計:Bash空格的那點事

高洛峰
高洛峰原創
2016-11-23 12:57:281193瀏覽

先了解下bash中什麼時候該用空格,什麼時候不該用。

1. 等號賦值兩邊不能有空格

2. 指令與選項之間需要空格

3. 管道兩邊空格可有可無

我們來看看常見的問題

1. 賦值時等號兩邊或只有左邊多了空格
test@pythontab.com ~ $ var1 = test
bash: var1: command not found
test@pythontab.com ~ $ echo ${var1:?error}
bash: var1: testerror
: test @pythontab.com ~ $ echo ${var1?error}
bash: var1: error
test@pythontab.com ~ $ var2 =test
bash: var2: command not found
test@pythontab.com ~ $ echo ${var2 :?error}
bash: var2: error
test@pythontab.com ~ $ echo ${var2?error}
bash: var2: error

這裡我用了bash的變數擴展,${var1:?error}當var1為unset或null(未定義或空)時, 報指定錯誤; ${var1?error}當var1為unset時,報指定錯誤。從執行結果來看,如果等號左邊有空格,則變數名當成指令執行,結果報command not found,變數沒有被賦值

2. 賦值時等號左邊沒有空格,右邊有空格(這種情況有點特別,你會發現兩種情況)
test@pythontab.com ~ $ var= test
test@pythontab.com ~ $ var= nocmd
bash: nocmd: command not found

同樣是等號右邊有空格,第一條命令沒報錯,而第二條報錯了。

這是因為shell中有這麼一種執行命令的方式: var=string command

命令command將得到變數var的值(至於在命令執行後,變數var的值是否保留下來,bash4中沒有保留,但我在dash中發現時保留下來的,不同的shell對這個的處理不同), 由於test是個命令,而nocmd不是,所以報了command not found.
test@pythontab.com ~ $ var=newtest eval echo $var
newtest
test@pythontab.com ~ $ echo $var

注意: 這裡我使用了eval, 是想避免在第一次解析時$var被替換成空字符串, 不然就會出現下面的情況(下面是錯誤的測試方法,在echo還沒執行時,$var已經被替換成空字串)

程式碼如下:
test@pythontab.com ~ $ var=newtest echo $var
test@pythontab.com ~ $ echo $var

到這裡,相信大家都明白了吧, 對於等號賦值,左右兩邊不可以有空格,雖然右邊有空格不一定報錯,但那絕對不是你想要的結果。

3. 指令和選項之間必須有空格

這個似乎大家都明白,為何我還這麼囉嗦呢?說到這裡,我不得不提一下一個非常特別的指令: [ 指令(你沒看錯,是[ ), 也就是test指令(當然bash中,這是個內建指令,但在這裡不影響

我們的理解)。或許你會覺得[命令眼熟,沒錯,我保證你見過它,來看看下面的例子
test@pythontab.com ~ $ if [ "abc" = "abc" ]; then echo 'they are the same '; fi
they are the same
test@pythontab.com ~ $ type -a [
[ is a shell builtin
[ is /usr/bin/[

想起來了吧? [指令常用到if判斷中,當然也有人喜歡這麼寫
test@pythontab.com ~ $ [ "abc" = "cba" ] || echo 'they are not the same'
they are not the same
test @pythontab.com ~ $ type -a [
[ is a shell builtin
[ is /usr/bin/[

[ 命令正名叫test命令,它們兩者幾乎一樣,為什麼不是完全一樣?來看看這個
test@pythontab.com ~ $ [ "abc" = "cba"
bash: [: missing `]'
test@pythontab.com ~ $ [ "abc" = "cba" ]
test@pythontab .com ~ $ test "abc" = "cba" ]
bash: test: too many arguments
test@pythontab.com ~ $ test "abc" = "cba"

清晰了吧,用[命令時,你必須給它一個尾巴], 用test指令時,就不能加個尾巴。尾巴]是[最後一個參數,不可缺少的參數, 代表[命令的結束

扯了這麼多,那到底這個和空格有毛關係?說這些,是先讓大家明白: [在shell中是個指令,它左右必須有空格! ]是[的最後不可缺少的參數,它兩邊也需要空格(雖然有些指令的參數能連一起,例如ps, 但[指令不行,它的參數之間必須有空格)。讓我們來看看關於[常見的錯誤

a. if 與[ 之間缺少空格
test@pythontab.com ~ $ if[ "$HOME" = "/home/igi"];then echo 'equal'; fi
bash: syntax error near unexpected token `then'
test@pythontab.com ~ $ if[ "$HOME" = "/home/igi" ];then echo 'equal'; fi
bash: syntax. then'
test@pythontab.com ~ $ if["$HOME" = "/home/igi"];then echo 'equal'; fi
bash: syntax error near unexpected token `then'
test@pythontab.com ~ $ if["$HOME" = "/home/igi" ];then echo 'equal'; fi
bash: syntax error near unexpected token `then'

語法分析錯誤,很明顯,if[ 對於bash來說,不知道是什麼鬼東西

b. [與後面的參數之間缺少空格
test@pythontab.com ~ $ if ["$HOME" = "/ home/igi" ];then echo 'equal'; fi
bash: [/home/igi: No such file or directory
test@pythontab.com ~ $ if ["$HOME" = "/home/igi"]; then echo 'equal'; fi
bash: [/home/igi: No such file or directory

["$HOME" 對bash來說,也不知道是什麼鬼東西

c. [ ] 之間的參數之間缺少空格
test@pythontab.com ~ $ if [ "abc"="abc" ]; then echo 'equal'; fi
equal
test@pythontab.com ~ $ if [ "abc"="cba" ] ; then echo 'equal'; fi
equal

第一條命令似乎是對的(實際上是正巧而已),看看第二條命令"abc" 和"cba"明顯不同,但卻判斷為相同。這是因為參數之間缺少了空格,被[指令認為內部是個值而已。看看下面的指令,你就會釋然
test@pythontab.com ~ $ if [ 0 ]; then echo 'equal'; fi
equal
test@pythontab.com ~ $ if [ "1" ]; then echo ' equal'; fi
equal
test@pythontab.com ~ $ if [ "" ]; then echo 'equal'; fi
test@pythontab.com ~ $ if [ ]; then echo 'equal'; fi

在[ ] 內部,如果只有一個值(那些因為缺少了空格而連一起的也算),不是空字串就為真。所以在[ ] 之間的參數,也要兩邊有空格,而不能堆一起

d. 參數和尾巴]之間缺少空格

這個就不羅嗦了,尾巴]也是[命令的參數,如同上面所講,參數之間必須有空格

扯了這麼多[命令與空格的事,但有些時候,缺了空格卻能正確運行, 當然這只是你好運, 一起來看看
test@pythontab. com ~ $ var=' abc'
test@pythontab.com ~ $ if [$var = "abc" ];then echo 'equal'; fi
equal
test@pythontab.com ~ $ if ["$var" = "abc" ];then echo 'equal'; fi
bash: [ abc: command not found

雙引號包圍起來的是一個整體,而沒雙引號的時候,字串前後的空格或製表符都被切開。如果恰巧你遇到了或你故意要丟棄字串前後的空格或製表符,那也不是不可能, 但非常不建議你這麼寫,你的程式碼將是非常脆弱的。

或是你該加的空格都加了,但還是報錯,這也可能和缺少雙引號有關。這樣的情況很普遍,最後再看
test@pythontab.com ~ $ var=''
test@pythontab.com ~ $ if [ "$var" = "abc" ];then echo 'equal'; fi
test@pythontab.com ~ $ if [ $var = "abc" ];then echo 'equal'; fi
bash: [: =: unary operator expected
test@pythontab.com ~ $ dvar='a b c'
test
test@pythontab.com ~ $ dvar='a b c'
test
test@pythontab.com ~ $ dvar='a b c'
test
test@pythontab.com ~ $ dvar='a b c'
test
test@pythontab.com ~ $ dvar='a b c'

test🎜test@pythontab.com ~ $ dvar='a b c'🎜test🎜test@pythontab.com ~ $ dvar='a b c'🎜test🎜test@p @pythontab.com ~ $ if [ $dvar = "a b c" ];then echo 'equal'; fi🎜bash: [: too many arguments🎜test@pythontab.com ~ $ if [ "$dvar" = "a b c" ] ;then echo 'equal'; fi🎜equal🎜🎜我再囉嗦一次,不要輕易省略雙引號。很清楚了吧?如果你還不明白,🎜🎜最後,對於管道兩邊可有可無的空格,就不扯淡了,因為沒遇到有人對此有疑惑.🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn