help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] bash lazy evaluation with -a


From: Maarten Billemont
Subject: Re: [Help-bash] bash lazy evaluation with -a
Date: Wed, 25 Jun 2014 22:32:17 -0400

As an aside, [[ does lazy evaluation:

[[ a = b && $(echo foo >&2) ]]

While [ does not:

[ a = b -a "$(echo foo >&2)” ]

That’s because [ cannot possibly, since it is parsed like an ordinary simple 
command: first its arguments are evaluated and then the command is executed.  
[[ on the other hand is a shell keyword which benefits from special parsing 
rules.  Notice how in [ you MUST quote the expansion, while in [[ you need not.

I tend to recommend that anyone who writes shell code that contains bashisms 
(such as -a) or has a hash bang that says “bash” in it, should NOT try to write 
“portable shell code”.  Their code will be much healthier, faster and better in 
all respects if they can just admit to themselves that bash scripts are not 
POSIX sh scripts.  When part of your code uses bashisms or the hash bang is 
bash, the script is not a POSIX script and shouldn’t confine itself to POSIX 
sh's primitive featureset.  On the other hand, if you value the ability of 
running the code you write in shells you’ve never even tested it in, by all 
means, try to write POSIX, put sh in your hash bang and cross your fingers 
(because that’s probably the best you can do to assure yourself the script is 
going to run fine in all POSIX shells).

— Maarten Billemont (lhunath) —
me: http://www.lhunath.com – business: http://www.lyndir.comhttp://masterpasswordapp.com

On Jun 24, 2014, at 17:01, Ken Goldman <address@hidden> wrote:

> On 6/24/2014 2:47 PM, Eric Blake wrote:
>> On 06/24/2014 10:45 AM, Ken Goldman wrote:
>>> (checking before I report a bug)
>> 
>> This is a bug in your code, not in bash.
> 
> Sorry I wasn't clear.  I never suspected a bug in bash.  I meant that I 
> wanted to check my analysis before I reported a but to the person (not me) 
> who wrote the script.
> 
> Thanks for all your help.
> 
> 
> 

Attachment: smime.p7s
Description: S/MIME cryptographic signature


reply via email to

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