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 ?
}
Using
[[ -z "$var" ]]
is the easiest way to know if a variable was set or not, but that option-z
doesn't distinguish between an unset variable and a variable set to an empty string:It's best to check it according to the type of variable: env variable, parameter or regular variable.
For a env variable:
For a parameter (for example, to check existence of parameter
$5
):For a regular variable (using an auxiliary function, to do it in an elegant way):
Notes:
I found a (much) better code to do this if you want to check for anything in
$@
.Why this all? Everything in
$@
exists in Bash, but by default it's blank, sotest -z
andtest -n
couldn't help you.Update: You can also count number of characters in a parameters.
To see if a variable is nonempty, I use
The opposite tests if a variable is either unset or empty:
To see if a variable is set (empty or nonempty), I use
The opposite tests if a variable is unset:
If
var
can be an array, then[ -z "${var+x}" ]
parameter substitution is incorrect. To be really sure in Bash you need to use array syntax like[ "${#var[@]}" = 0 ]
, as shown below.In almost all cases, they agree. The only situation I've found where the array method is more accurate is when the variable is a non-empty array with position
0
unset (e.g., in tests7
andA
below). This disagreement comes from$var
being shorthand for${var[0]}
, so[ -z "${var+x}" ]
isn't checking the whole array.Here are my test cases.
Here's the output.
In sum:
${var+x}
parameter expansion syntax works just as well as${#var[@]}
array syntax in most cases, such as checking parameters to functions. The only way this case could break is if a future version of Bash adds a way to pass arrays to functions without converting contents to individual arguments.0
unset.declare -a var
has been used without assigning even a null value somewhere in the array. Bash still distinguishes the case somewhere (as seen in testB
above), so this answer's not foolproof. Fortunately Bash converts exported environment variables into strings when running a program/script, so any issues with declared-but-unset variables will be contained to a single script, at least if it's not sourcing other scripts.To check whether a variable is set with a non-empty value, use
[ -n "$x" ]
, as others have already indicated.Most of the time, it's a good idea to treat a variable that has an empty value in the same way as a variable that is unset. But you can distinguish the two if you need to:
[ -n "${x+set}" ]
("${x+set}"
expands toset
ifx
is set and to the empty string ifx
is unset).To check whether a parameter has been passed, test
$#
, which is the number of parameters passed to the function (or to the script, when not in a function) (see Paul's answer).