Clearing variables

If you want to assign a variable an empty value, you can do so by leaving the right side of an assignment to the variable blank:

$ system=

However, the variable name still appears in the list of variables, it just has an empty value:

bash$ declare -p system
declare -- system=""

The system variable is defined, but empty. POSIX shell script actually does not make much of a distinction between these two states. It's more common to test whether a variable is empty than it is to check whether it's defined:

#!/bin/bash
# If the 'system' variable is not empty, print its value to the user
if [[ -n $system ]] ; then
    printf 'The "system" variable is: %s\n' "$system"
fi

You can actually remove a variable with unset:

$ unset -v system
$ declare -p system
bash: declare: system: not found

If you do ever need to check whether a variable is defined, Bash allows you to do this with the -v test; note that we useĀ system, not $system, here:

#!/bin/bash
# If the "system" variable has been set--even if it's empty--print its
# value to the user
if [[ -v system ]] ; then
    printf 'The "system" variable is: %s\n' "$system"
fi