bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: When double quote is considered when not?


From: Greg Wooledge
Subject: Re: When double quote is considered when not?
Date: Wed, 30 Mar 2011 08:12:09 -0400
User-agent: Mutt/1.4.2.3i

On Wed, Mar 30, 2011 at 02:43:58PM +0430, ali hagigat wrote:
> ----------------------------------
> var1="word1     word2"
> echo $var1
> if (test -z "\"$var1\"")   then
> echo "first"
> else
> echo second
> fi

Problems in this script:

 * Unquoted $var1 on line 2.  This means that instead of passing the actual
   content of the variable to echo, bash is going to split it into words,
   perform globbing (filename expansion) on each of those words, and then
   pass the resulting lists of words to echo.

 * Unnecessary subshell invocation (parentheses) on line 3.  There's
   no reason to create a new process there -- all it does is slow down
   the script.

 * You are testing the length of a string that will NEVER be zero-length.
   The string you are testing has two literal quotation marks in it, so
   it will ALWAYS be at least 2 characters long.  The "test -z" will
   therefore never be true.

> ----------------------------------
> var1="word1     word2"
> echo $var1
> if (test -z ""$var1"")   then
> echo "first"
> else
> echo second
> fi

Problems in this script:

 * Unquoted $var1 on line 2, same as before.

 * Unquoted $var1 on line 3.  This is even worse than the case on line 2,
   because when you pass multiple arguments to the test command following
   a -z switch, you will generate an error condition.

 * Unnecessary subshell, just as before.

 * Unnecessary use of double double quotes on line 3.  All they are doing
   is increasing the length of the code.  They have no effect.  $var1
   and ""$var1"" and """"""""$var1"""""""" are all the same thing.  Adding
   zero-length strings to a string does not change the string, no matter
   how many of them you add.

Here is what you SHOULD have done:

var1="what   ever"
echo "$var1"
if test -z "$var1"; then
  echo empty
else
  echo not empty
fi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]