所以我做对了吗?在Bash中,如果我在条件周围使用双括号,我可以使用globbing来测试字符串的模式:
if [[ $var = abc* ]]
如果我使用单括号,我可以测试多种条件:
if [ $var = abc123 -o $var = abc456 ]
但我无法一起做到这两件事?
我正在使用Bash v3.0,因此它无法识别正则表达式或=〜运算符。
答案 0 :(得分:1)
POSIX标准不推荐使用-o
。对于POSIX兼容性,请使用两个单独的test
命令:
if [ "$var" = abc123 ] || [ "$var" = abc456 ]
在bash
中,您可以使用[ ... ]
或[[ ... ]]
命令执行相同操作,或在||
命令中使用[[ ... ]]
:
if [ "$var" = abc123 ] || [ "$var" = abc456 ] # POSIX compatible
# No reason to use either of the next three, but the syntax is valid
if [[ "$var" = abc123 ]] || [ "$var" = abc456 ]
if [ "$var" = abc123 ] || [[ "$var" = abc456 ]]
if [[ "$var" = abc123 ]] || [[ "$var" = abc456 ]]
if [[ "$var" = abc123 || "$var" = abc456 ]] # Preferred for bash
答案 1 :(得分:0)