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.

10
  • 8
    what's the inverse of -z? if not empty string. Commented Jul 19, 2013 at 23:48
  • 149
    if [ ! -z "$VAR" ]; Commented Jul 22, 2013 at 17:26
  • 374
    the inverse of -z is -n if [ -n "$VAR" ]; Commented Oct 9, 2013 at 5:59
  • 31
    The double quotes ensure that the variable doesn't get split. A simple $var on a command line will be split by its whitespace into a list of parameters, while "$var" will always be only one parameter. Quoting variables often is a good practice and stops you from tripping up on filenames containing whitespace (among other things). Example: after doing a="x --help", try cat $a - it will give you the help page for cat. Then try cat "$a" - it will (usually) say cat: x --help: No such file or directory. In short, quote early and quote often and you will almost never regret it. Commented Jul 15, 2014 at 13:40
  • 2
    I feel like your answer requires a caveat - namely if your bash script has something like set -u then the answer will break/fail at that point. There is a subtle (and usually ignored) difference between unset and empty. Note that the distinction is only really important if it's important to you, or if the script has already set -u.
    – Jon V
    Commented Feb 8, 2017 at 16:01