How can I do something like
set variable (some_command_that_may_return_a_string)
if [ variable is set ]
do_something
and inversely how do I check if the variable is empty?
How can I do something like
set variable (some_command_that_may_return_a_string)
if [ variable is set ]
do_something
and inversely how do I check if the variable is empty?
set -q var
(note the missing "$" - this uses the variable name) can be used to check if a variable has been set.set -q var[1]
can be used to check whether the first element of a variable has been assigned (i.e. whether it is non-empty as a list).test -n "$var"
[fn0] (or[ -n "$var" ]
) can be used to check whether a variable expands to a non-empty string (andtest -z
is the inverse - true if it is empty).These will be true/false in slightly different circumstances.
When no
set var
has been performed at all (and it has not been inherited from the parent process),set -q var
,set -q var[1]
andtest -n "$var"
will be false,test -z "$var"
will be true.When something like
set var
has been done (without any additional arguments),set -q var
will be true,set -q var[1]
will be false.When something like
set var ""
has been done, bothset
versions will be true.When something like
set var "somestring"
(or evenset var "" ""
[fn1]) has been done, theset
s will be true andtest -z "$var"
will be false.[fn0]: You never want to use
test
(or[
) without quoting the variable. One particularly egregious example is thattest -n $var
will return true both if the variable contains something and if it is list-empty/unset (noset
at all orset var
without arguments). This is because fish'stest
is one of the few parts that follow POSIX, and that demands thattest
with any one argument be true. Also it does not handle lists properly -test -n $var
will have weird results if var has more than one element.[fn1]: This is because a list will be expanded as a string by joining the elements with spaces, so the list consisting of two empty strings will expand to " " - one space. Since that isn't empty,
test -z
returns false.