foo() any-command
is the Bourne syntax supported by any Bourne-like shell but bash
, yash
and recent versions of posh
(which only support compound commands). (the Bourne shell and AT&T implementations of ksh
don't support foo() any-command > redirections
unless any-command
is a compound command though).
foo() any-compound-command
(examples of compound commands: { cmd; }
, for i do echo "$i"; done
, (cmd)
... the most commonly used being { ...; }
)
is the POSIX syntax supported by any Bourne-like shell and the one you generally want to use.
function foo { ...; }
is the Korn shell syntax, which predates the Bourne syntax. Only use this one if writing specifically for the AT&T implementation of the Korn shell and need the specific treatment it receives there. That syntax is not POSIX, but is supported by bash
, yash
and zsh
for compatibility with the Korn shell though those shells (and the pdksh
-based variants of the Korn shell) don't treat it any different from the standard syntax.
function foo () { ...; }
is the syntax of no shell and should not be used. It only happens to be supported by accident by bash
, yash
, zsh
and the pdksh
based variants of the Korn shell. Incidentally, it's also the awk
function syntax.
If we continue going down the esoteric list,
function foo() other-compound-command
(like function foo() (subshell)
or function foo() for i do; ... done
) is even worse. It is supported by bash
, yash
and zsh
, but not ksh, even the pdksh
-based variants.
While:
function foo() simple command
is only supported by zsh
.
foo bar() any-command
Or even:
$function_list() any-command
To define several functions at once is only supported by zsh
function { body; } args
() any-compound-command args
Which are anonymous function invocations, are only supported by zsh
as well.
function baz { echo "baz"; }
. See Bashism in GreyCat's wiki.