确定bash中的variables是否为空(“”)的最佳方法是什么?
我听说,build议我这样做if [ "x$variable" = "x" ]
那是正确的方法吗? (必须有更直接的东西)
如果一个variables未被设置或设置为空string(“”),这将返回true。
if [ -z "$VAR" ];
在Bash中,当你不关心对不支持它的shell的可移植性时,你应该总是使用双括号语法:
以下任何一项:
if [[ -z $variable ]] if [[ -z "$variable" ]] if [[ ! $variable ]] if [[ ! "$variable" ]]
在Bash中,使用双方括号,引号是没有必要的。 您可以简化对包含值的variables的testing:
if [[ $variable ]]
这个语法与ksh兼容(至lessksh93,无论如何)。 它在纯粹的POSIX或较老的Bourne shell(如sh或dash)中不起作用。
有关双方括号和单方括号之间差异的更多信息,请参阅我的答案和BashFAQ / 031 。
您可以testing以查看某个variables是否被特别设置(与空string不同):
if [[ -z ${variable+x} ]]
其中“x”是任意的。
如果你想知道一个variables是否为空而不是未设置:
if [[ -z $variable && ${variable+x} ]]
bash中的variables(以及任何POSIX兼容的shell)可以处于以下三种状态之一:
大多数情况下,您只需要知道variables是否设置为非空string,但偶尔区分未设置和设置为空string很重要。
以下是如何testing各种可能性的示例,它可以在bash或任何与POSIX兼容的shell中使用:
if [ -z "${VAR}" ]; then echo "VAR is unset or set to the empty string" fi if [ -z "${VAR+set}" ]; then echo "VAR is unset" fi if [ -z "${VAR-unset}" ]; then echo "VAR is set to the empty string" fi if [ -n "${VAR}" ]; then echo "VAR is set to a non-empty string" fi if [ -n "${VAR+set}" ]; then echo "VAR is set, possibly to the empty string" fi if [ -n "${VAR-unset}" ]; then echo "VAR is either unset or set to a non-empty string" fi
这是同样的事情,但方便的表格forms:
+-------+-------+-----------+ VAR is: | unset | empty | non-empty | +-----------------------+-------+-------+-----------+ | [ -z "${VAR}" ] | true | true | false | | [ -z "${VAR+set}" ] | true | false | false | | [ -z "${VAR-unset}" ] | false | true | false | | [ -n "${VAR}" ] | false | false | true | | [ -n "${VAR+set}" ] | false | true | true | | [ -n "${VAR-unset}" ] | true | false | true | +-----------------------+-------+-------+-----------+
如果VAR
未设置,则${VAR+foo}
构造展开为空string;如果VAR
设置为任何值(包括空string),则展开为foo
。
如果设置(包括设置为空string),则${VAR-foo}
构造展开为VAR
的值,如果未设置,则展开为foo
。 这对于提供用户可覆盖的默认值是很有用的(例如, ${COLOR-red}
表示使用red
除非variablesCOLOR
被设置为某个值)。
通常build议[ x"${VAR}" = x ]
用于testingvariables是未设置还是设置为空string的原因是因为某些[
命令(也称为test
)的实现是错误的。 如果将VAR
设置为-n
,那么在给定[ "${VAR}" = "" ]
时,某些实现会执行错误的操作,因为[ "${VAR}" = "" ]
的第一个参数被错误地解释为-n
操作符,而不是string。
-z
是最好的方法。
我用过的另一个选项是设置一个variables,但是可以被另一个variables覆盖
export PORT=${MY_PORT:-5432}
如果$MY_PORT
variables为空,那么PORT
被设置为5432,否则PORT被设置为MY_PORT
的值。 注意语法包括冒号和破折号。
如果您有兴趣区分set-empty和unset status的情况,请查看bash的-u选项:
$ set -u $ echo $BAR bash: BAR: unbound variable $ [ -z "$BAR" ] && echo true bash: BAR: unbound variable $ BAR="" $ echo $BAR $ [ -z "$BAR" ] && echo true true
另一个我见过的[ -z "$foo" ]
是以下内容,但我不确定为什么人们使用这种方法,任何人都知道?
[ "x${foo}" = "x" ]
无论如何,如果你不允许未设置的variables(通过set -u
或set -o nounset
),那么你会遇到这两种方法的麻烦。 有一个简单的解决方法:
[ -z "${foo:-}" ]
注意:这将使您的variablesundef。
这个问题问如何检查一个variables是否是一个空string,并已经给出了最好的答案。
但是,在经过了一段时间的PHP编程之后,我到达了这里,而实际上我正在search的东西是像在bash shell中工作的php中的空白函数 。
在阅读答案后,我意识到我没有在bash中正确思考,但无论如何,在PHP这样的空函数将在我的bash代码中非常方便。
正如我认为这可能发生在其他人身上,我决定在bash中转换php空function
根据php手册 :
如果一个variables不存在或者它的值是下列值之一,则该variables被认为是空的:
当然null和false的情况下不能在bash中转换,所以它们被省略了。
function empty { local var="$1" # Return true if: # 1. var is a null string ("" as empty string) # 2. a non set variable is passed # 3. a declared variable or array but without a value is passed # 4. an empty array is passed if test -z "$var" then [[ $( echo "1" ) ]] return # Return true if var is zero (0 as an integer or "0" as a string) elif [ "$var" == 0 2> /dev/null ] then [[ $( echo "1" ) ]] return # Return true if var is 0.0 (0 as a float) elif [ "$var" == 0.0 2> /dev/null ] then [[ $( echo "1" ) ]] return fi [[ $( echo "" ) ]] }
使用示例:
if empty "${var}" then echo "empty" else echo "not empty" fi
演示:
以下片段:
#!/bin/bash vars=( "" 0 0.0 "0" 1 "string" " " ) for (( i=0; i<${#vars[@]}; i++ )) do var="${vars[$i]}" if empty "${var}" then what="empty" else what="not empty" fi echo "VAR \"$var\" is $what" done exit
输出:
VAR "" is empty VAR "0" is empty VAR "0.0" is empty VAR "0" is empty VAR "1" is not empty VAR "string" is not empty VAR " " is not empty
话虽如此,在bash逻辑中,在这个函数中的零检查可能会导致一些问题,任何使用这个函数的人都应该评估这个风险,也许会决定把这些检查关掉,只留下第一个。
整个if-then和-z是不必要的。
[“$ foo”] && echo“foo is not empty” [“$ foo”] || 回声“foo确实是空的”
个人更喜欢更清晰的方式来检查:
if [ "${VARIABLE}" == "" ]; then echo VARIABLE is empty else echo VARIABLE is not empty fi
确切地说,当$ FOO被设置并且为空时,
[ "${FOO+x}" = x ] && [ -z "$FOO" ]
我的5美分:还有比if ...
更短的语法,这一个:
VALUE="${1?"Usage: $0 value"}"
如果提供了一个参数,这一行将设置VALUE,并且在出现错误的情况下将打印一个带有脚本行号的错误消息(并且将终止脚本执行)。
另一个例子可以在abs-guide中find (search“Example 10-7”)。
在duffbeer703的解决scheme的oneliner扩展:
#! /bin/bash [ -z "$1" ] || some_command_that_needs_$1_parameter