The following bash syntax verifies if param isn't empty: [[ !  -z  $param  ]]For example:param=[[ !  -z  $param  ]] && echo I am not zeroNo output and its fine.But when param is empty except for one (or more) space characters, then the case is different:param=  # one space[[ !  -z  $param  ]] && echo I am not zeroI am not zero is output.How can I change the test to consider variables that contain only space characters as empty?
How can I test if a variable is empty or contains only spaces?
bash;shell;shell script;string
First, note that the -z test is explicitly for:the length of string is zeroThat 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.