BASH question

Ken Burtch ken-8VyUGRzHQ8IsA/PxXw9srA at public.gmane.org
Fri Jun 11 20:55:44 UTC 2010


Hmmm...didn't know they added "==" to Bash but I haven't bought Chris' 
book yet either.  Sounds like feature creep/bloat to me.  I can understand 
Bash's function command, but adding "==" doesn't serve much of a purpose, 
especially when it the semantics aren't identical with "==" in other 
computer languages.

The double equals is common in most C-based languages, including PHP, C++ 
and Java.  The shell standard is a single equals sign.  As Chris suggests, 
use single equals sign for portability unless you have some compelling 
reason otherwise.

I agree with William that $ans should be in double quotes: if $ans is 
empty, it will crash your script.  If you want minimum 
keystrokes, William is correct that the quotes around the q are not 
necessary.  Quotes in shell scripts are used for word deliniation, not for 
string representation, so q, "q" and 'q' are all equivalent (a single 
character).  I usually wrap things like the q in double quotes so I can 
use word expansion later, if I need it, but it is not required.

Ken B.

-----------------------------------------------------------------------------
Ken O. Burtch                                       Phone/Fax: 905-562-0848
   "Linux Shell Scripting with Bash"                  Email: ken at pegasoft.ca
                                     Blog: http://www.pegasoft.ca/coder.html
-----------------------------------------------------------------------------

On Fri, 11 Jun 2010, Chris F.A. Johnson wrote:

> On Fri, 11 Jun 2010, bob 295 wrote:
>
>> Recently some of my online students have been reporting errors surrounding the
>> if statement in the (simplified) code snip below.
>>
>> ======= begin snip =========
>>
>>         echo -n "Which test do you wish to run? (suggest s0001) [q to exit] "
>>         read ans
>>         if [ $ans == 'q' ]
>>         then
>>                 echo "got quit"
>>         else
>>                 echo "got $ans"
>>         fi
>>
>> ======  end snip ===========
>>
>> My Linux in a Nutshell reference (circa 2000) claims that the double equals is
>> the proper syntax.     However, if I search online I find that a single
>> equals also works and that the double equals is a synonym for the single
>> variation.
>>
>> Which is the proper form?    Was this changed in BASH?  If so when and what is
>> the recommended way to handle older and newer versions?
>
>   The standard is a single =; bash also accepts ==.
>
>   When there is no improvement (functionality or efficiency) gained
>   by the bash-specific form, I always use the portable syntax.
>
> --
>   Chris F.A. Johnson, <http://cfajohnson.com>
>   Author:
>   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
>   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)


More information about the Legacy mailing list