bug-bash
[Top][All Lists]
Advanced

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

Re: wrong logical evaluation of expressions involving true or false comm


From: Bob Proulx
Subject: Re: wrong logical evaluation of expressions involving true or false commands
Date: Mon, 25 Jun 2007 14:07:56 -0600
User-agent: Mutt/1.5.9i

Miguel Ángel wrote:
> Evaluating expressions like:
> [ true -a false ] ; echo $?
> always returns 0 (true), doesn't mind if I change true for false or
> viceversa.

In the case of the test operator (aka '[ expr ]') use of "true" and
"false" as you have done are strings and not boolean expressions and
not commands.

Saying '[ true -a false ]' is asking if "true" is a string non-zero in
length and if "false" is a string non-zero in length.  Both are true
and therefore the expression is true.  This would be the same as the
following.  These are the same.

  [ foo -a bar ]
  [ -n foo -a -n bar ]

This is defined by standard by the number of arguments.

  http://www.opengroup.org/onlinepubs/009695399/utilities/test.html

  1 argument:
      Exit true (0) if $1 is not null; otherwise, exit false.

Therefore all of the following must be true because they have one
argument and are not null.

  [ foo ]
  [ false ]
  [ FALSE ]

This next would be false.

  [ "" ]

> any expression wich involves the true or false commands throws wrong
> results.
> Examples:
> 
> [ "a" = "a" -a false ] ; echo $?
> 
> gives 0 (true)

In order to use the true and false commands as booleans they must be
called in the position of a command.

  true && false ; echo $?

  [ "a" = "a" ] && false ; echo $?

  istrue=true
  if $istrue; then echo istrue; else echo isnottrue; fi

  istrue=false
  if $istrue; then echo istrue; else echo isnottrue; fi

Bob




reply via email to

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