Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • 3
    Testing for a variable set to the empty string can also be done using [ -z "${VAR-set}" ].
    – nwellnhof
    Commented May 17, 2013 at 17:56
  • @nwellnhof: Thanks! I updated my answer to use the briefer syntax. Commented May 18, 2013 at 18:00
  • 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. Commented Nov 11, 2014 at 19:51
  • 4
    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:-}" ].
    – Kevin Jin
    Commented Sep 14, 2017 at 14:12