Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I define a shell function as function x( ) or just x( )?

Tags:

bash

shell

sh

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.

like image 400
whitfin Avatar asked Mar 06 '14 23:03

whitfin


People also ask

How do you define a shell function?

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.

What does X mean in shell script?

The "x" has no special meaning and could be replaced with any non-null string.

How do you define and call a function in shell script?

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 [${*}]..." }

Can we use function in shell script?

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.


6 Answers

A bit of history

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.

like image 193
Henk Langeveld Avatar answered Oct 28 '22 03:10

Henk Langeveld


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.

like image 23
nwk Avatar answered Oct 28 '22 01:10

nwk


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

like image 42
Mike Holt Avatar answered Oct 28 '22 03:10

Mike Holt


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.

like image 37
v-a Avatar answered Oct 28 '22 01:10

v-a


There is a big difference: Portability. The function keyword is not POSIX, and only supported by bash and ksh.

like image 28
Jan Groth Avatar answered Oct 28 '22 02:10

Jan Groth


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.

like image 31
anubhava Avatar answered Oct 28 '22 03:10

anubhava