bug-bash
[Top][All Lists]
Advanced

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

Re: Bug#741402: [bash] Shell functions definitions may require parenthes


From: Dan Douglas
Subject: Re: Bug#741402: [bash] Shell functions definitions may require parentheses despite function keyword
Date: Fri, 14 Mar 2014 00:34:40 -0500
User-agent: KMail/4.12.3 (Linux/3.13.2-pf+; KDE/4.12.3; x86_64; ; )

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

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