Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 29 Jun 2001 02:32:51 -0500
From:      Mike Meyer <mwm@mired.org>
To:        Dave Tweten <tweten@nas.nasa.gov>
Cc:        questions@freebsd.org
Subject:   Re: Bourne Shell Syntax Wierdness
Message-ID:  <15164.12067.829448.734806@guru.mired.org>
In-Reply-To: <98860201@toto.iv>

next in thread | previous in thread | raw e-mail | index | archive | help
Dave Tweten <tweten@nas.nasa.gov> types:
> The sh man page says that the two operators "||" and "&&" have the
> same precedence.  It also says that "a || b" means "execute b if a
> terminates abnormally" and "a && b" means "execute b if a terminates
> normally."
>
> So I don't understand why "true || true && echo Oops!" prints "Oops!"

The || and && operators are left associative, so that your example -
which is ambiguous given only the information you provided - is parsed
as "( true || true ) && echo Oops!". Since "( true || true )" always
terminates normally, it should print "echo Oops!".

> This is apparently not a bug in FreeBSD sh, because IRIX, IRIX64, and SunOS 
> Bourne/Korn shells work the same way, but it certainly runs counter to my 
> ability to read English and the contents of the man page.

But it matches the way most people read expressions. The world would
be a better place if we switched to an unambiguous system for
mathematical expressions :-).

> Incidently, "true || { true && echo Oops! }" prints nothing -- which
> I would have expected with or without the "{}".

That's what would happen if the operators were right associative
instead of left associative - which would collide with most peoples
expectations based on common mathematical usage.

	<mike
--
Mike Meyer <mwm@mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15164.12067.829448.734806>