我知道我可以用-z
測試一個字符串是否爲空,並用-n
測試字符串是否爲非空。所以我寫在Ubuntu 10.10的腳本:bash空字符串比較問題
#!/bin/bash
A=
test -z $A && echo "A is empty"
test -n $A && echo "A is non empty"
test $A && echo "A is non empty"
str=""
test -z $str && echo "str is empty"
test -n $str && echo "str is non empty"
test $str && echo "str is non empty"
令我驚訝的是,它的輸出:
A is empty
A is non empty
str is empty
str is non empty
我的事情應該是
A is empty
str is empty
可以在任何Linux的專家解釋,爲什麼?
謝謝。