Using bash, what's the best method to check if a variable is empty or not?
If I use:
if [ -z "$VAR" ]
as suggested in a forum this works for an unset variable but it is true when the variable is set but empty. Suggestions?
Use unset command to delete the variables during program execution. It can remove both functions and shell variables.
To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"
Unsetting Variables Once you unset a variable, you cannot access the stored value in the variable. Following is the syntax to unset a defined variable using the unset command − unset variable_name. The above command unsets the value of a defined variable.
You can quickly test for null or empty variables in a Bash shell script. You need to pass the -z or -n option to the test command or to the if command or use conditional expression.
Unset (non-existant) variables and empty variables behaves differently in parameter expansion:
In the following examples:
Without colon:
Checks only for variable existence.
With colon:
Checks for variable existence, if it exists, make sure that it's not empty.
In other words, checks both for variable existence and non-emptiness.
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
${parameter-word}
If parameter is unset...
${parameter:=word}
If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.
${parameter=word}
If parameter is unset...
${parameter:?word}
If parameter is unset or null, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.
${parameter?word}
If parameter is unset...
${parameter:+word}
If parameter is unset or null, nothing is substituted, otherwise the expansion of word is substituted.
${parameter+word}
If parameter is unset...
Source
${var+set}
substitutes nothing if the variable is unset and set
if it is set to anything including the empty string. ${var:+set}
substitutes set
only if the variable is set to a non-empty string. You can use this to test for either case:
if [ "${foo+set}" = set ]; then
# set, but may be empty
fi
if [ "${foo:+set}" = set ]; then
# set and nonempty
fi
if [ "${foo-unset}" = unset ]; then
# foo not set or foo contains the actual string 'unset'
# to avoid a potential false condition in the latter case,
# use [ "${foo+set}" != set ] instead
fi
if [ "${foo:-unset}" = unset ]; then
# foo not set or foo empty or foo contains the actual string 'unset'
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With