HOWTO · Bash
How to Check if a Variable Has a Value in Bash
This tutorial will discuss checking if a variable contains a value in Bash.
On this page
A script often needs to validate user input before it acts on it. In Bash, “has a value” can mean either that a variable contains a nonempty string or that it has been assigned at all. Those are different questions: an unset variable and a variable assigned '' both expand to an empty string, but only the latter is set.
Understand what counts as a value
Use a nonempty-string check when the script needs content, such as a file name or user response. Use a set-state check when an explicitly supplied empty value has a different meaning from omitted input. Decide which state matters before choosing an operator; otherwise, a correct test can still enforce the wrong requirement.
Test for an empty or nonempty string with -z and -n
The Bash conditional operators -z and -n test a string’s length. -z succeeds when the quoted expansion is empty; -n succeeds when it is nonempty. Quoting "$value" ensures the test receives one string even when the value contains spaces or wildcard characters. Use the lower-case variable name consistently: Bash variable names are case-sensitive.
Use the result in an if...else statement
Run this example:
name="Ada"
if [[ -n "$name" ]]; then
printf 'Hello, %s\n' "$name"
else
printf 'Please enter a name.\n'
fi
The condition is true for this assigned name, so the script takes the first branch. The else branch handles a value that does not meet the test. In Bash, [[ ... ]] is a conditional command; the portable-style [ ... ] form can also test -z and -n when its arguments are quoted, as in [ -n "$name" ]. Do not use == as a substitute for -z or -n when the requirement is about string length.
Distinguish an unset variable with parameter expansion
To ask whether a variable is set, use the ${value+x} parameter expansion. It expands to x when value is set, even if its value is an empty string, and expands to nothing when it is unset. This avoids comparing the variable to a sentinel such as unset, which could also be legitimate input.
unset value
if [[ ${value+x} ]]; then
printf 'The variable is set.\n'
else
printf 'The variable is unset.\n'
fi
The parameter expansion is inside [[ ... ]], where it is tested as a string. To check the other state, assign value='' before the conditional: the set-state test then takes the first branch, while [[ -z "$value" ]] still succeeds. This illustrates why “set” and “nonempty” should not be treated as synonyms.
Choose the check that matches your requirement
Use [[ -n "$value" ]] when your script needs a nonempty string, and [[ -z "$value" ]] when it should detect an empty string. Use [[ ${value+x} ]] when the distinction is whether an assignment exists, including an assignment to an empty string. The negated form [[ ! -n "$value" ]] also tests for an empty string, but -z states that intent more directly.
These examples require Bash syntax and should run in a Bash shell; Linux is not the only operating system on which Bash is available. Pick the check that matches the input contract of your script, then handle each result in the branch that makes sense for the task.