Test for non-zero length string in bash: [ -n “$var” ] or [ “$var” ]?

Edit: This is a more complete version that shows more differences between (aka test ) and .

Edit: This is a more complete version that shows more differences between (aka test) and . The following table shows that whether a variable is quoted or not, whether you use single or double brackets and whether the variable contains only a space are the things that affect whether using a test with or without -n/-z is suitable for checking a variable. 1a 2a 3a 4a 5a 6a |1b 2b 3b 4b 5b 6b " -n -n" -z -z" | " -n -n" -z -z" unset: false false true false true true |false false false false true true null : false false true false true true |false false false false true true space: false true true true true false |true true true true false false zero : true true true true false false |true true true true false false digit: true true true true false false |true true true true false false char : true true true true false false |true true true true false false hyphn: true true true true false false |true true true true false false two : -err- true -err- true -err- false |true true true true false false part : -err- true -err- true -err- false |true true true true false false Tstr : true true -err- true -err- false |true true true true false false Fsym : false true -err- true -err- false |true true true true false false T= : true true -err- true -err- false |true true true true false false F= : false true -err- true -err- false |true true true true false false T!

= : true true -err- true -err- false |true true true true false false F! = : false true -err- true -err- false |true true true true false false Teq : true true -err- true -err- false |true true true true false false Feq : false true -err- true -err- false |true true true true false false Tne : true true -err- true -err- false |true true true true false false Fne : false true -err- true -err- false |true true true true false false If you want to know if a variable is non-zero length, do any of the following: quote the variable in single brackets (column 2a) use -n and quote the variable in single brackets (column 4a) use double brackets with or without quoting and with or without -n (columns 1b - 4b) Notice in column 1a starting at the row labeled "two" that the result indicates that is evaluating the contents of the variable as if they were part of the conditional expression (the result matches the assertion implied by the "T" or "F" in the description column). When is used (column 1b), the variable content is seen as a string and not evaluated.

The errors in columns 3a and 5a are caused by the fact that the variable value includes a space and the variable is unquoted. Again, as shown in columns 3b and 5b, evaluates the variable's contents as a string. If you're using , the key to making sure that you don't get unexpected results is quoting the variable.

Using , it doesn't matter. The error messages, which are being suppressed, are "unary operator expected" or "binary operator expected". This is the script that produced the table above.

#! /bin/bash # by Dennis Williamson # 2010-10-06, revised 2010-11-10 # for http://stackoverflow.com/questions/3869072/test-for-non-zero-length-string-in-bash-n-var-or-var # designed to fit an 80 character terminal dw=5 # description column width w=6 # table column width t () { printf "%-${w}s" "true"; } f () { $? == 1 && printf "%-${w}s" "false " || printf "%-${w}s" "-err- "; } o=/dev/null echo ' 1a 2a 3a 4a 5a 6a |1b 2b 3b 4b 5b 6b' echo ' " -n -n" -z -z" | " -n -n" -z -z"' while read -r d t do printf "%-${dw}s: " "$d" case $d in unset) unset t ;; space) t=' ' ;; esac $t 2>$o && t || f "$t" && t || f -n $t 2>$o && t || f -n "$t" && t || f -z $t 2>$o && t || f -z "$t" && t || f echo -n "|" $t && t || f "$t" && t || f -n $t && t || f -n "$t" && t || f -z $t && t || f -z "$t" && t || f echo done T= 1 = 1 F= 1 = 2 T!

= 1! = 2 F! = 1!

= 1 Teq 1 -eq 1 Feq 1 -eq 2 Tne 1 -ne 2 Fne 1 -ne 1 EOF.

Wow.. very good explanation! +1 – jyzuz Oct 6 '10 at 11:11 Thanks! I've decided to adopt the style of "quote the variable in single brackets (column 2a)" IMO, the -n just adds noise and decreases readability.

Similarly, for testing for zero-length or unset, I'll use! "$var" instead of -z "$var" . – AllenHalsey Oct 6 '10 at 20:32.

Use case/esac to test case "$var" in "") echo "zero length";; esac.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions