On Wednesday, March 12, 2014 01:57:19 AM Filipus Klutiero wrote:
> The following definition should therefore be valid:
> > $ function bar (echo x)
> 
> ...but it's not:
> > bash: syntax error near unexpected token `echo'
> 
> However, an identical function can be defined with the following definition:
> 
> > $ function bar () (echo x)
> 

This is a documentation issue at most. That function should be defined either
as:

        function bar {
                ( echo x )
        }

or

        bar() (
                echo x
        )


Bash (and zsh and mksh) allow the "function name()" syntax. The former syntax
exists for ksh93 compatibility. ksh93 considers the curly braces to be part of
the function definition syntax itself, whereas Bash considers them to be a
command group compound command (which is why in bash they can be omitted).
Therefore, you should always use the curly braces when defining a function that
way.

Most people advise using the POSIX syntax, unless there's a specific reason
to use the "function name { ... }" syntax (Bash treats them identically).
Never use the "function name () ..." syntax. IMO it shouldn't even be
documented.  It causes endless confusion but the reason it exists isn't clear.
There are other alternate syntaxes that are also discouraged, but undocumented,
such as the "for ((;;)) { ...; }" syntax.

-- 
Dan Douglas

Reply via email to