Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
If it’s worthwhile to examine if a bash
variable is empty, or unset, then you should use the next code:
if [ -z "${VAR}" ];
The above code will examine if a variable referred to as VAR
is ready, or empty.
Unset
signifies that the variable has not been set.
Empty
signifies that the variable is ready with an empty worth of ""
.
-z
?The inverse of -z
is -n
.
if [ -n "$VAR" ];
VALUE="${1?"Utilization: $0 worth"}"
if [[ -z ${VAR+x} ]]
if [ -z "${VAR}" ]; then
echo "VAR is unset or set to the empty string"
fi
if [ -z "${VAR+set}" ]; then
echo "VAR is unset"
fi
if [ -z "${VAR-unset}" ]; then
echo "VAR is ready to the empty string"
fi
if [ -n "${VAR}" ]; then
echo "VAR is ready to a non-empty string"
fi
if [ -n "${VAR+set}" ]; then
echo "VAR is ready, probably to the empty string"
fi
if [ -n "${VAR-unset}" ]; then
echo "VAR is both unset or set to a non-empty string"
fi
This implies:
+-------+-------+-----------+
VAR is: | unset | empty | non-empty |
+-----------------------+-------+-------+-----------+
| [ -z "${VAR}" ] | true | true | false |
| [ -z "${VAR+set}" ] | true | false | false |
| [ -z "${VAR-unset}" ] | false | true | false |
| [ -n "${VAR}" ] | false | false | true |
| [ -n "${VAR+set}" ] | false | true | true |
| [ -n "${VAR-unset}" ] | true | false | true |
+-----------------------+-------+-------+-----------+