Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I can define bash functions using or omitting the function keyword. Is there any difference?

#!/bin/bash

function foo() {
  echo "foo"
}

bar() {
  echo "bar"
}

foo

bar

Both calls to functions foo and bar succeed and I can't see any difference. So I am wondering if it is just to improve readability, or there is something that I am missing...

BTW in other shells like dash (/bin/sh is symlinked to dash in debian/ubuntu) it fails when using the function keyword.

share|improve this question
6  
Also with or without the parenthesis: function baz { echo "baz"; }. See Bashism in GreyCat's wiki. –  manatwork Apr 26 '13 at 11:34

4 Answers 4

up vote 9 down vote accepted

There is no difference AFAIK, other than the fact that the second version is more portable.

share|improve this answer
4  
That's a BIG difference, portability... I see too many answers which simply do NOT work on (older) production systems, and also many with options that only work on linux. At least, warn that this is not the most portable way... It could be downright dangerous (asking someone to tar cf - /some/thing | ssh user@desthost "cd destinationdir && tar xf - " without warning them to first double-check if the version of tar on desthost will get rid of the "/" could lead to disasters in some cases...). For ex: if use a function tar { #a safe tar with safety checks ... } and sh ignores it, ... –  Olivier Dulac Apr 26 '13 at 15:51

The function keyword was introduced in ksh. The traditional Bourne shell only had the foo () syntax, and POSIX standardizes only the foo () syntax.

In ATT ksh (but not pdksh), there are a few differences between functions defined by function and functions defined with the Bourne/POSIX syntax. In functions defined by function, the typeset keyword declares a local variable: once the function exits, the value of the variable is reset to what it was before entering the function. With the classic syntax, variables have a global scope whether you use typeset or not.

$ ksh -c 'a=global; f () { typeset a=local; }; f; echo $a'
local
$ ksh -c 'a=global; function { typeset a=local; }; f; echo $a'
global

Another difference in ksh is that functions defined with the function keyword have their own trap context. Traps defined outside the function are ignored while executing the function, and fatal errors inside the function exit only the function and not from the whole script. Also, $0 is the function name in a function defined by function but the script name in a function defined with ().

Pdksh does not emulate ATT ksh. In pdksh, typeset creates locally-scoped variables regardless of the function, and there are no local traps (though using function does make some minor differences — see the man page for details).

Bash and zsh introduced the function keyword for compatibility with ksh. However in these shells function foo { … } and foo () { … } are strictly identical, as is the bash and zsh extension function () { … }. The typeset keyword always declares local variables, and traps are not local (you can get local traps in zsh by setting the local_traps option).

share|improve this answer
1  
It should be noted that function support was added to the Korn shell before the Bourne shell introduced its foo() command syntax, and the Bourne syntax was later added to the Korn shell for compatibility. –  Stéphane Chazelas Apr 29 '13 at 11:54

Semantically, those two forms are equivalent in Bash.

From the man page:

Shell functions are declared as follows:

name () compound-command [redirection]
function name [()] compound-command [redirection]

This defines a function named name. The reserved word function is optional. If the function reserved word is supplied, the parentheses are optional.

EDIT: I just noticed that this question is tagged posix. In POSIX sh, the function keyword is not used (though it is reserved).

share|improve this answer
foo() any-command

is the Bourne syntax supported by any Bourne-like shell but bash and yash (which only support compound commands).

foo() any-compound-command

is the POSIX syntax supported by any Bourne-like shell and the one you 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 and zsh for compatibility with the Korn shell though those shells 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, zsh and the pdksh based variants of the Korn shell. Incidentally, it's also the awk function syntax.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.