Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [BUG] Condition in [[ doesn't fire, with "if" it fires
- X-seq: zsh-users 22515
- From: Peter Stephenson <p.stephenson@xxxxxxxxxxx>
- To: Zsh Users' List <zsh-users@xxxxxxx>
- Subject: Re: [BUG] Condition in [[ doesn't fire, with "if" it fires
- Date: Tue, 28 Feb 2017 11:51:58 +0000
- Cms-type: 201P
- In-reply-to: <1488281256.2881475.895357184.6106CE5C@webmail.messagingengine.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- Organization: Samsung Cambridge Solution Centre
- References: <1488280289.2878360.895324176.3971EE76@webmail.messagingengine.com> <CGME20170228112804epcas2p2e241ff0013d6ac1cae21987a41442ce8@epcas2p2.samsung.com> <1488281256.2881475.895357184.6106CE5C@webmail.messagingengine.com>
On Tue, 28 Feb 2017 03:27:36 -0800
Sebastian Gniazdowski <psprint3@xxxxxxxxxxxx> wrote:
> Checked that even:
>
> [[ "below" = "below" ]] && (( line ++ )) || (( line = stborder ))
>
> doesn't fire. In the same way. It is the false option that is fired:
Ah, I see what you've done now. I moved this to zsh-users because this
is an interesting point and the above should give folks enough context
to see what I'm talking about; look at zsh-workers for more background.
&& and || don't work the way they do in C, they are simply evaluated
left to right in a symmetric fashion. (Yes, another shell oddity dating
from year zero for Ray to moan about.) Have a look at the grammar tests
for some example.
In detail:
- Test succeeds.
- So (( line ++ )) run
- line was zero, and with a post increment the value is zero, so status
is 1.
- (This is the unexpected bit.) On a non-zero status, the shell looks
forward for the next ||, ignoring any &&'s. As I said, there is no
relative priority, just a strict left to right evaluation.
- So it then executes (( line = stborder ))
- I'm guessing stborder is 0, so that's what line is set to.
You *could* fix this up by switching to a pre-increment, but I'd
recommend just going to if/then/else --- it's much more obvious what the
logic actually means.
pws
Messages sorted by:
Reverse Date,
Date,
Thread,
Author