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: Stephane Chazelas
Subject: Re: [Help-bash] Interesting consequence of Bash's grammar
Date: Thu, 27 Aug 2015 20:37:51 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2015-08-27 16:09:38 +0000, Ioan-Andrei Bârsan:
> Hello everyone,
> 
> I was reading through Bash's source code today and looking at its grammar
> (in `parse.y') when I noticed something peculiar. The following rule caught
> my eye:
> 
> function_body:    shell_command
>             { $$ = $1; }
> 
> 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? Wouldn't it make more sense to define
> `function_body' based on `group_command' instead of `shell_command'?
[...]

The Bourne shell syntax to define a function is to stick
funcname() in front of a command.

It's common there to do things like:

la() ls -la "$@"

That's recognised by every Bourne like shell but bash, yash and
recent versions of posh, which only allow compound commands
there which is the minimum required by POSIX.

Earlier versions of bash only recognised

foo() { code; }

I beleive bash was modified to be POSIX-conformant which is why 
it only recognised compound commands and not simple commands
like other shells.

I once enquired on the Austin Group mailing list (the guys
behind POSIX) on the reason why the POSIX specification was
funcname() compound_command and not funcname() any-command, but
nobody could come up with an explanation.

-- 
Stephane




reply via email to

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