Timeline for How to determine if a bash variable is empty?
Current License: CC BY-SA 4.0
13 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
S Aug 1, 2023 at 12:46 | history | suggested | Nick Dong | CC BY-SA 4.0 |
"because" is duplicate to "the reason". Add comma mark for long clause.
|
Jul 20, 2023 at 7:22 | review | Suggested edits | |||
S Aug 1, 2023 at 12:46 | |||||
Sep 14, 2017 at 14:12 | comment | added | Kevin Jin |
The unset checks aren't reliable. If the user called set -u or set -o nounset in bash, then the test will just result in the error "bash: VAR: unbound variable". See stackoverflow.com/a/13864829 for a more reliable unset check. My go-to check for whether a variable is null or unset is [ -z "${VAR:-}" ] . My check for whether a variable is non-empty is [ "${VAR:-}" ] .
|
|
Mar 11, 2016 at 21:54 | review | Suggested edits | |||
Mar 11, 2016 at 23:24 | |||||
Nov 11, 2014 at 19:51 | comment | added | André Laszlo |
In bash, there's also a difference between ${VAR+foo} and ${VAR:+foo} . The former would evaluate to foo only if VAR is unset, and the version with : will also return foo if VAR is set to the empty string.
|
|
Jul 22, 2013 at 17:07 | history | edited | Richard Hansen | CC BY-SA 3.0 |
add a table for easy reference
|
May 18, 2013 at 18:38 | history | edited | Richard Hansen | CC BY-SA 3.0 |
added 513 characters in body
|
May 18, 2013 at 18:00 | comment | added | Richard Hansen | @nwellnhof: Thanks! I updated my answer to use the briefer syntax. | |
May 18, 2013 at 17:59 | history | edited | Richard Hansen | CC BY-SA 3.0 |
added 2 characters in body
|
May 18, 2013 at 17:54 | history | edited | Richard Hansen | CC BY-SA 3.0 |
simplify the "VAR is set to the empty string" test
|
May 17, 2013 at 17:56 | comment | added | nwellnhof |
Testing for a variable set to the empty string can also be done using [ -z "${VAR-set}" ] .
|
|
Nov 18, 2012 at 17:29 | history | edited | Richard Hansen | CC BY-SA 3.0 |
added 191 characters in body
|
Apr 24, 2012 at 20:39 | history | answered | Richard Hansen | CC BY-SA 3.0 |