How do I know if a variable is set in Bash?
For example, how do I check if the user gave the first parameter to a function?
function a {
# if $1 is set ?
}
How do I know if a variable is set in Bash?
For example, how do I check if the user gave the first parameter to a function?
function a {
# if $1 is set ?
}
Functions to check if variable is declared/unset
including empty
$array=()
The following functions test if the given name exists as a variable
$1
contains the name of an empty$array=()
, the call to declare would make sure we get the right resultThis functions would test as showed in the following conditions:
.
Read the "Parameter Expansion" section of the
bash
man page. Parameter expansion doesn't provide a general test for a variable being set, but there are several things you can do to a parameter if it isn't set.For example:
will set
first_arg
equal to$1
if it is assigned, otherwise it uses the value "foo". Ifa
absolutely must take a single parameter, and no good default exists, you can exit with an error message when no parameter is given:(Note the use of
:
as a null command, which just expands the values of its arguments. We don't want to do anything with$1
in this example, just exit if it isn't set)For those that are looking to check for unset or empty when in a script with
set -u
:The regular
[ -z "$var" ]
check will fail withvar; unbound variable
ifset -u
but[ -z "${var-}" ]
expands to empty string ifvar
is unset without failing.My prefered way is this:
So basically, if a variable is set, it becomes "a negation of the resulting
false
" (what will betrue
= "is set").And, if it is unset, it will become "a negation of the resulting
true
" (as the empty result evaluates totrue
) (so will end as beingfalse
= "NOT set").I like auxiliary functions to hide the crude details of bash. In this case, doing so adds even more (hidden) crudeness:
Because I first had bugs in this implementation (inspired by the answers of Jens and Lionel), I came up with a different solution:
I find it to be more straight-forward, more bashy and easier to understand/remember. Test case shows it is equivalent:
The test case also shows that
local var
does NOT declare var (unless followed by '='). For quite some time I thought i declared variables this way, just to discover now that i merely expressed my intention... It's a no-op, i guess.BONUS: usecase
I mostly use this test to give (and return) parameters to functions in a somewhat "elegant" and safe way (almost resembling an interface...):
If called with all requires variables declared:
else:
I always find the POSIX table in the other answer slow to grok, so here's my take on it:
Note that each group (with and without preceding colon) has the same set and unset cases, so the only thing that differs is how the empty cases are handled.
With the preceding colon, the empty and unset cases are identical, so I would use those where possible (i.e. use
:=
, not just=
, because the empty case is inconsistent).Headings:
VARIABLE
is non-empty (VARIABLE="something"
)VARIABLE
is empty/null (VARIABLE=""
)VARIABLE
does not exist (unset VARIABLE
)Values:
$VARIABLE
means the result is the original value of the variable."default"
means the result was the replacement string provided.""
means the result is null (an empty string).exit 127
means the script stops executing with exit code 127.$(VARIABLE="default")
means the result is the original value of the variable and the replacement string provided is assigned to the variable for future use.