HOWTO · Bash
How to Check Whether a Bash Variable Is Empty
Learn four Bash ways to test a variable for an empty string, including safe quoting and how unset and whitespace-only values behave.
On this page
For a supplied Bash value, quote its expansion so the shell passes it as one argument. This script prints a labeled value:
value='Bash'
if [ -z "$value" ]; then
printf '%s\n' 'No value supplied'
else
printf 'Value: %s\n' "$value"
fi
Value: Bash
The test command (also written as [ ... ]) returns success when the expanded string has zero characters. An unset variable expands to an empty string in a normal Bash shell, so -z treats unset and explicitly empty values alike. With set -u, use ${value-} to provide a default before testing. A value such as 0 or a string of spaces is nonempty; the test checks characters, not whether a word looks truthy.
Check Whether a Bash Variable Is Empty with -z
The test command (also written as [ ... ]) returns a successful status when the expanded string has zero characters. A nonzero status means it contains at least one character. The test itself does not print a message; the if statement selects which command to run.
value=''
if [ -z "$value" ]; then
printf '%s\n' 'value is empty'
else
printf '%s\n' 'value is not empty'
fi
value is empty
For an unset variable, the same quoted check succeeds in the usual shell configuration. With set -u, write [ -z "${value-}" ] instead so expansion remains defined. The bracket command is a shell command whose success or failure is represented by its exit status; it does not print true or false automatically.
Check Whether a Bash Variable Is Nonempty with -n
Use -n for the complementary test: it returns success when the expanded string has at least one character. The then branch runs for a nonempty value, and else runs when the test fails, including when the value is empty.
value='Bash'
if [ -n "$value" ]; then
printf '%s\n' 'value is not empty'
else
printf '%s\n' 'value is empty'
fi
value is not empty
Quoting matters because it preserves the expansion as one argument. As with -z, use ${value-} inside the quotes if the script may run with set -u and the variable may be unset. The -n option is especially readable when the next action should happen only after a value has been provided.
Compare a Bash Variable with an Empty String
You can compare a value directly with an empty string using Bash’s [[ ... ]] conditional expression. The quoted expansion makes the intent clear, and a string containing spaces is still nonempty because those spaces are characters.
value=' '
if [[ "$value" == "" ]]; then
printf '%s\n' 'value is empty'
else
printf '%s\n' 'value is not empty'
fi
value is not empty
This example takes the second branch: spaces do not disappear inside [[ ... ]]. For scripts that use set -u, compare ${value-} so an unset variable is handled as an empty string.
Check a Variable with ${parameter:+word} Parameter Expansion
The ${parameter:+word} form expands to word when the parameter is set and non-null. It expands to nothing when the parameter is unset or contains an empty string. Test the expansion while quoting it:
value='Bash'
if [ "${value:+present}" ]; then
printf '%s\n' 'value is set and nonempty'
else
printf '%s\n' 'value is unset or empty'
fi
value is set and nonempty
This idiom is useful when the distinction you need is “set and nonempty” versus “unset or empty.” It does not distinguish those latter two states. To tell an unset parameter apart from a set-but-empty one, use Bash’s [[ -v value ]] check as a separate test.