2

You can write a bash functions several ways:

function JoinStrings {
 ...;
}

Or

function JoinStrings () {
 ...;
}

Or

JoinStrings () {
 ...;
}

Is there any difference between these functions? Why are there 3 different ways to write a function in bash? (Are there more ways to write function?)

asked Dec 4, 2015 at 0:39
2
  • see unix.stackexchange.com/a/74045 Commented Dec 4, 2015 at 0:46
  • One important point is that name() {...} is POSIX, and more universal. Since all notations accomplish the same thing, I prefer to use the most portable (universal) notation as specified by POSIX. Commented Dec 4, 2015 at 3:38

1 Answer 1

0

man bash says:

Shell Function Definitions
 A shell function is an object that is called like a simple command and exe‐
 cutes a compound command with a new set of positional parameters. 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. The body of the function is the compound command com‐
 pound-command (see Compound Commands above). That command is usually
 a list of commands between { and }, but may be any command listed
 under Compound Commands above. compound-command is executed whenever
 name is specified as the name of a simple command. When in posix
 mode, name may not be the name of one of the POSIX special builtins.
 Any redirections (see REDIRECTION below) specified when a function is
 defined are performed when the function is executed. The exit status
 of a function definition is zero unless a syntax error occurs or a
 readonly function with the same name already exists. When executed,
 the exit status of a function is the exit status of the last command
 executed in the body. (See FUNCTIONS below.)

In short, no there is no difference.

answered Dec 4, 2015 at 1:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.