help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Interesting consequence of Bash's grammar


From: Eric Blake
Subject: Re: [Help-bash] Interesting consequence of Bash's grammar
Date: Thu, 27 Aug 2015 13:32:37 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0

On 08/27/2015 11:54 AM, Greg Wooledge wrote:
> On Thu, Aug 27, 2015 at 04:09:38PM +0000, Ioan-Andrei Bârsan wrote:
>> shell_command can, among other things, be the `{ ... }` group command, but
>> it can also be an if statement, or a while loop (see line 760). This means
>> that the following statement is legal Bash:
>>
>> bar () if true; then echo "I am a weird funciton."; fi
>>
>> Is this a known/intentional feature?
> 
> Yes.  Some people even use it on purpose, like:
> 
> foo() (
>   # This function always runs in a subshell, so we can run amok with
>   # transient side effects.
>   shopt -s stuff
>   cd somewhere
>   PATH=things
>   ...
> )

And it is required by POSIX:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_05

> The format of a function definition command is as follows:
> 
> fname() compound-command[io-redirect ...]

so changing the grammar to require {} would break conforming scripts.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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