Skip to main content
15 events
when toggle format what by license comment
Apr 15 at 9:19 comment added metablaster Any valid reason for if [ -z "${VAR}" ]; instead of if [ -z "$VAR" ]; isn't first one for arrays?
Mar 31, 2022 at 7:38 comment added Timo More info on -n as the inverse of -z from man test: -n string True if the length of string is nonzero.
May 24, 2020 at 12:49 history edited 030 CC BY-SA 4.0
added 2 characters in body
Nov 17, 2019 at 7:41 comment added Sang In Bash, [[ -v VAR]] can be used to check if VAR is defined, [[ -z $VAR ]] is to check if "$VAR" is expanded to null string (""). Thus, [[ -v VAR && -z $VAR ]] should be the correct answer. read more here (with official reference)
Aug 4, 2019 at 15:13 comment added Biggybi in if [ -n "$VAR" ];, quotes ensure returning 0 for zero-lenght strings.
Feb 8, 2017 at 16:01 comment added Jon V 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.
S Mar 4, 2015 at 7:19 history suggested Steve Bennett CC BY-SA 3.0
add the important missing half sentence
Mar 4, 2015 at 5:30 review Suggested edits
S Mar 4, 2015 at 7:19
Jul 15, 2014 at 13:40 comment added Score_Under 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.
Oct 9, 2013 at 5:59 comment added Felipe Alvarez the inverse of -z is -n if [ -n "$VAR" ];
Jul 22, 2013 at 17:26 comment added Aaron Copley if [ ! -z "$VAR" ];
Jul 19, 2013 at 23:48 comment added Jürgen Paul what's the inverse of -z? if not empty string.
Sep 1, 2012 at 1:37 comment added Aaron Newton That is true - I was going to recommend -s, but test -s '' exits with a 1, whereas test -z '' gives us the anticipated 0.
May 12, 2009 at 18:39 vote accept Brent
May 12, 2009 at 18:06 history answered duffbeer703 CC BY-SA 2.5