I've noticed that there are three ways of defining shell functions, and I've never seen it explained anywhere.
# Option 1
function log(){
}
# Option 2
log(){
}
# Option 3, added due to answers
function log{
}
Is there a different between these three definitions? Do they behave differently, or is it just the look?
Is there a standard to which should be used? I would expect that the first option would be preferable, as I imagine it removes ambiguity somewhere down the line.
Thanks in advance!
Edit After Answer: Whoever voted to close as "opinion-based"; I asked for the difference in behaviour, not just why people think I should use which. There is a major difference in the fact that one is only supported in Bash, which means that everybody should aim to be using the second option.
Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed.
The "x" has no special meaning and could be replaced with any non-null string.
You need to define your functions before you call them. Using () : process_install() { echo "Performing process_install() commands, using arguments [${*}]..." } process_exit() { echo "Performing process_exit() commands, using arguments [${*}]..." }
A shell function may do neither, either or both. It is generally accepted that in shell scripts they are called functions. A function may return a value in one of four different ways: Change the state of a variable or variables.
For absolute Bourne shell and POSIX compatibility, use log () { ...; }
. This is especially important in older unix systems and small linux distros that tend to include minimal shell implementations.
There was a special reason for David Korn to introduce the function
keyword. He wanted to add
features to functions in ksh
(ksh93) and wanted to make a clear distinction between normal functions and enhanced functions.
In ksh93
, functions defined with function name { ...; }
can use local variables, if they're
declared with typeset var=value
.
Bash uses the keywords declare
and local
, and afaik they work in both types of functions. Even typeset
is a synonym for declare
.
The mixed-up syntax, both function
and ()
, is not compatible with either Bourne, ksh, or POSIX.
In my own ksh code, I tend to use lots of functions, and local variables tend to improve the code.
If I expect the code to be reused in a more generic shell than ksh93
I will revert to POSIX style functions and use subshells to force variables to be local.
The downside to options 1 and 3 is that they both are so-called "bashisms". If you want cross-shell compatibility (e.g., with sh
in *BSDs) use option 2 because any POSIX shell must support it.
According to the Bash Hackers Wiki, the function
keyword is useless and should never be used, because it's not portable. Apparently, only bash and other ksh-derived shells allow both, and the function
keyword is a now-obsolete throwback to ksh-style functions. The following references give a better explanation:
http://wiki.bash-hackers.org/scripting/obsolete
http://www.gnu.org/software/bash/manual/bashref.html#Shell-Functions
http://tldp.org/LDP/abs/html/functions.html
log()
is supported by the Bourne Shell family and any type of derivate (dash,yash) Is the POSIX std syntax and probably the one you want to use to write something compatible with old systems. Probably is the one you want to use.
function log () { ...; }
Is supported by bash and zsh but using both function
and the ()
is simply WRONG and should be avoided.
function log { ...; }
Is the Korn Shell syntax and is supported by bash and zsh for compatibility reasons, but is not POSIX
A quote from http://wiki.bash-hackers.org/scripting/obsolete about "function log ()"
This is an amalgamation between the Korn and POSIX style function definitions - using both the function keyword and parentheses. It has no useful purpose and no historical basis or reason to exist. It is not specified by POSIX. It is accepted by Bash, mksh, zsh, and perhaps some other Korn shells, where it is treated as identical to the POSIX-style function. It is not accepted by AT&T ksh. It should never be used. See the next table for the function keyword. Bash doesn't have this feature documented as expressly deprecated.
There is a big difference: Portability. The function
keyword is not POSIX, and only supported by bash
and ksh
.
That is just your personal call since both syntaxes work fine. But this one:
log() {
echo "logs"
}
is the original Bourne and MODERN POSIX syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With