Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
To check if variable has only spaces, including in a multi-linesline variable with spaces, try this:
[[ $var = *[$" \t\n"]* ]]
Or with xargs:
[[ -z $(echo $var | xargs) ]]
Or with sed:
[[ -z $(sed "/^$/d" <<< $var) ]]
To check a multi-lines variable with spaces, try this:
To check if variable has only spaces, including in a multi-line variable, try this:
ForTo check a multi-lines variable with spaces, try this:
[[ $var = *[$'*[$" \t\n']*\t\n"]* ]]
[[ -z $(sed '"/^$/d'd" <<< $var) ]]
For multi-lines variable with spaces, try this:
[[ $var = *[$' \t\n']* ]]
[[ -z $(sed '/^$/d' <<< $var) ]]
With regex there's an ultimate way to check forFor multi-lines variable with spaces, try this:
[[ $var =~= \s**[$' \t\n']* ]]
Unlike other approaches here, it also includes multi-lines variableOr with spaces.sed:
With regex there's an ultimate way to check for spaces:
[[ $var =~ \s* ]]
Unlike other approaches here, it also includes multi-lines variable with spaces.