[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Interesting consequence of Bash's grammar
From: |
Ioan-Andrei Bârsan |
Subject: |
Re: [Help-bash] Interesting consequence of Bash's grammar |
Date: |
Thu, 27 Aug 2015 19:43:42 +0000 |
This is very interesting. I will go through the standard and keep reading
the Bash source code.
Thank you very much for the answers!
Kind regards,
Andrei
On Thu, Aug 27, 2015 at 10:32 PM Eric Blake <address@hidden> wrote:
> 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
>
>