[ale] Bash confusion

The Don Lachlan ale-at-ale.org at unpopularminds.org
Wed Jul 20 15:22:40 EDT 2011


On Wed, Jul 20, 2011 at 01:04:10PM -0400, Brian MacLeod wrote:
> You are doing a string comparison here, so you should take the correct
> precautions:
> > if [ $date = "01" ];
> should be like this:
> if [ "01" = "$date" ];
> Why?
> If for some ungodly reason your variable ends up as null, the whole
> statement (and function, etc) breaks if you have that null in front of
> the equal sign.  Flip that around and it "properly" evaluates even if
> null. Otherwise you'll see things like "unary operator" and such.
> The other thing is making sure you treat your variable as a string,
> thus, quote it so that you are sure it isn't being further evaluated
> and/or evaluated differently.

Order is irrelevent; quoting the string protects against null values.

  if [ "$date" = "01" ]

is safe, while

  if [ "01" = $date ]

fails if $date is a null string.


Regarding the original problem, as written, yes, it should work. But...

Are there any commands between the date command and the test? There's no
explicit path used to date - could it be executing a different version of
date which is evaluating improperly? IOW, what are you telling us? ;)

-L


More information about the Ale mailing list