How can I test if a variable is empty or contains only spaces?
First, note that the -z
test is explicitly for:
the length of string is zero
That is, a string containing only spaces should not be true under -z
, because it has a non-zero length.
What you want is to remove the spaces from the variable using the pattern replacement parameter expansion:
[[ -z "${param// }" ]]
This expands the param
variable and replaces all matches of the pattern (a single space) with nothing, so a string that has only spaces in it will be expanded to an empty string.
The nitty-gritty of how that works is that ${var/pattern/string}
replaces the first longest match of pattern
with string
. When pattern
starts with /
(as above) then it replaces all the matches. Because the replacement is empty, we can omit the final /
and the string
value:
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with ‘/’, all matches of pattern are replaced with string. Normally only the first match is replaced. ... If string is null, matches of pattern are deleted and the / following pattern may be omitted.
After all that, we end up with ${param// }
to delete all spaces.
Note that though present in ksh
(where it originated), zsh
and bash
, that syntax is not POSIX and should not be used in sh
scripts.
The easy way to check that a string only contains characters in an authorized set is to test for the presence of unauthorized characters. Thus, instead of testing whether the string only contains spaces, test whether the string contains some character other than space. In bash, ksh or zsh:
if [[ $param = *[!\ ]* ]]; then
echo "\$param contains characters other than space"
else
echo "\$param consists of spaces only"
fi
“Consists of spaces only” includes the case of an empty (or unset) variable.
You may want to test for any whitespace character. Use [[ $param = *[^[:space:]]* ]]
to use locale settings, or whatever explicit list of whitespace characters you want to test for, e.g. [[ $param = *[$' \t\n']* ]]
to test for space, tab or newline.
Matching a string against a pattern with =
inside [[ … ]]
is a ksh extension (also present in bash and zsh). In any Bourne/POSIX-style, you can use the case
construct to match a string against a pattern. Note that standard shell patterns use !
to negate a character set, rather than ^
like in most regular expression syntaxes.
case "$param" in
*[!\ ]*) echo "\$param contains characters other than space";;
*) echo "\$param consists of spaces only";;
esac
To test for whitespace characters, the $'…'
syntax is specific to ksh/bash/zsh; you can insert these characters in your script literally (note that a newline will have to be within quotes, as backslash+newline expands to nothing), or generate them, e.g.
whitespace=$(printf '\n\t ')
case "$param" in
*[!$whitespace]*) echo "\$param contains non-whitespace characters";;
*) echo "\$param consists of whitespace only";;
esac
POSIXly:
case $var in
(*[![:blank:]]*) echo '$var contains non blank';;
(*) echo '$var contains only blanks or is empty or unset'
esac
To differentiate between blank, non-blank, empty, unset:
case ${var+x$var} in
(x) echo empty;;
("") echo unset;;
(x*[![:blank:]]*) echo non-blank;;
(*) echo blank
esac
[:blank:]
is for horizontal spacing characters (space and tab in ASCII, but there are probably a few more in your locale; some systems will include the non-breaking space (where available), some won't). If you want vertical spacing characters as well (like newline or form-feed), replace [:blank:]
with [:space:]
.