Difference between myFun () and myFun function
As a new user (ish) to bash, what is the difference between myFun()
and funtion myFun
? I came across both, not only in people's code but also in tutorials. Is there any difference between the two? When I tried this, nothing similar happens, so I'm pretty sure the two methods for defining a function are just syntactically different and don't execute differently, but can someone confirm this assumption?
source to share
There is a big difference between these two syntaxes for defining functions:
name() compound-command
function name compound-command
The first is POSIX and therefore widely portable. The latter is not the case. Otherwise, they are identical.
Examples of
dash
is the default wrapper ( /bin/sh
) for debian-like systems. Note that with dash
this method the function definition is successful:
$ fn() { date; }
$ fn
Mon Nov 24 14:27:49 PST 2014
But this method is not like this:
$ function fn { date; }
dash: 2: function: not found
Similar error in ash
(busybox shell):
$ function fn { date; }
-sh: function: not found
source to share