Timeline for How can I test if a variable is empty or contains only spaces?
Current License: CC BY-SA 3.0
13 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 14, 2021 at 22:38 | comment | added | Lee Meador | Yes ${myVar// } works. Also ${myVar%%*( )} and ${myVar##*( )} and ${myVar##+( )} and its because if we remove leading, trailing or all spaces in a string it will become empty if it only contains spaces. | |
| May 6, 2020 at 15:54 | comment | added | Noam Manos |
This approach will not catch multi-line variable with spaces. Try xargs instead: [[ -z $(echo $var | xargs) ]]
|
|
| Dec 8, 2017 at 7:11 | comment | added | Brian Chrisman | bash note: if running in set -o nounset (set -u), and param is unset (rather than null or space-filled), then this will generate an unbound variable error. ( set +u; ..... ) would exempt this test. | |
| Jun 9, 2017 at 0:59 | comment | added | Jesse Chisholm |
@MichaelHomer The answer [[ -z "${param// }" ]] sure looks like the pattern is empty and the replacement string is a single space. AH! I see it now if pattern begins with a slash I missed that part. so the pattern is / and the string is left off and therefore empty. Thanks.
|
|
| May 22, 2017 at 19:21 | comment | added | Michael Homer | @JesseChisholm "Because the replacement is empty, we can omit the final / and the string value"; "If string is null, matches of pattern are deleted and the / following pattern may be omitted." | |
| May 22, 2017 at 17:51 | comment | added | Jesse Chisholm |
Hmm. If it is ${var/pattern/string} then shouldn't it be [[ -z ${param/ /} ]] with the space between the slashes to be the pattern and nothing after to be the string?
|
|
| May 15, 2017 at 23:57 | comment | added | mikezter |
use sed they said... just echo the variable they said...
|
|
| S Jun 30, 2016 at 15:13 | history | suggested | m0meni | CC BY-SA 3.0 |
fixed stuff
|
| Jun 30, 2016 at 14:40 | review | Suggested edits | |||
| S Jun 30, 2016 at 15:13 | |||||
| Jan 21, 2016 at 0:25 | history | edited | Michael Homer | CC BY-SA 3.0 |
Fix missing ] reported by luqmaan
|
| Jul 30, 2014 at 6:05 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 |
added 152 characters in body
|
| Jul 28, 2014 at 9:57 | vote | accept | maihabunash | ||
| Jul 28, 2014 at 9:30 | history | answered | Michael Homer | CC BY-SA 3.0 |