How to tell if a string is not defined in a bash s

2019-01-10 00:18发布

If I want to check for the null string I would do

[ -z $mystr ]

but what if I want to check whether the variable has been defined at all? Or is there no distinction in bash scripting?

11条回答
狗以群分
2楼-- · 2019-01-10 00:48

another option: the "list array indices" expansion:

$ unset foo
$ foo=
$ echo ${!foo[*]}
0
$ foo=bar
$ echo ${!foo[*]}
0
$ foo=(bar baz)
$ echo ${!foo[*]}
0 1

the only time this expands to the empty string is when foo is unset, so you can check it with the string conditional:

$ unset foo
$ [[ ${!foo[*]} ]]; echo $?
1
$ foo=
$ [[ ${!foo[*]} ]]; echo $?
0
$ foo=bar
$ [[ ${!foo[*]} ]]; echo $?
0
$ foo=(bar baz)
$ [[ ${!foo[*]} ]]; echo $?
0

should be available in any bash version >= 3.0

查看更多
贼婆χ
3楼-- · 2019-01-10 00:52

https://stackoverflow.com/a/9824943/14731 contains a better answer (one that is more readable and works with set -o nounset enabled). It works roughly like this:

if [ -n "${VAR-}" ]; then
    echo "VAR is set and is not empty"
elif [ "${VAR+DEFINED_BUT_EMPTY}" = "DEFINED_BUT_EMPTY" ]; then
    echo "VAR is set, but empty"
else
    echo "VAR is not set"
fi
查看更多
我命由我不由天
4楼-- · 2019-01-10 00:53

not to shed this bike even further, but wanted to add

shopt -s -o nounset

is something you could add to the top of a script, which will error if variables aren't declared anywhere in the script. The message you'd see is unbound variable, but as others mention it won't catch an empty string or null value. To make sure any individual value isn't empty, we can test a variable as it's expanded with ${mystr:?}, also known as dollar sign expansion, which would error with parameter null or not set.

查看更多
祖国的老花朵
5楼-- · 2019-01-10 00:55

call set without any arguments.. it outputs all the vars defined..
the last ones on the list would be the ones defined in your script..
so you could pipe its output to something that could figure out what things are defined and whats not

查看更多
女痞
6楼-- · 2019-01-10 00:56

Advanced bash scripting guide, 10.2. Parameter Substitution:

  • ${var+blahblah}: if var is defined, 'blahblah' is substituted for the expression, else null is substituted
  • ${var-blahblah}: if var is defined, it is itself substituted, else 'blahblah' is substituted
  • ${var?blahblah}: if var is defined, it is substituted, else the function exists with 'blahblah' as an error message.


to base your program logic on whether the variable $mystr is defined or not, you can do the following:

isdefined=0
${mystr+ export isdefined=1}

now, if isdefined=0 then the variable was undefined, if isdefined=1 the variable was defined

This way of checking variables is better than the above answer because it is more elegant, readable, and if your bash shell was configured to error on the use of undefined variables (set -u), the script will terminate prematurely.


Other useful stuff:

to have a default value of 7 assigned to $mystr if it was undefined, and leave it intact otherwise:

mystr=${mystr- 7}

to print an error message and exit the function if the variable is undefined:

: ${mystr? not defined}

Beware here that I used ':' so as not to have the contents of $mystr executed as a command in case it is defined.

查看更多
登录 后发表回答