2

The function definition syntax for various languages is:

C (the godfather of all scripting languages):

func_type myfunc_c (arg_type arg_name , ...)
{
    /* arguments explicitly specified */
}

TCL:

proc myfunc_tcl {arg1 arg2 args} {
    # arguments explicitly specified
}

Perl:

sub myfunc_perl {
    # no arguments explicitly specified && no round brackets used
}

Python:

def myfunc_python(arg1, arg2):
    # arguments explicitly specified

Bash:

function myfunc_bash () {
    # arguments NEVER explicitly specified
    # WHY using round brackets?
}

Why using round brackets in bash?

typo
  • 230
  • 1
  • 11
Eugeniu Rosca
  • 5,177
  • 16
  • 45
  • 6
    your description of the C language is going to upset quite a few people.. :) – Pynchia Jun 08 '15 at 15:08
  • 1
    Where does it say you have to have the parentheses in bash? The [Bash HOWTO doesn't use them](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html).. – Martijn Pieters Jun 08 '15 at 15:09
  • @Pynchia: I've added the [c] tag to counterbalance the downvotes! :) I will not give up on my beliefs!! :D Martijn Pieters && ThisSuitIsBlackNot: thanks for the precious info, guys! – Eugeniu Rosca Jun 08 '15 at 15:24
  • 1
    Posix shell requires () but bash allows the `function` keyword instead. And neither require {}. (The body needs to be a *compound command*. Try this, for example: `f() for a; do echo Argument "$a"; done` – rici Jun 08 '15 at 15:34
  • 1
    It would possibly be better if you asked why the POSIX standard requires them when Bash does not, particularly when they appear to have no functionality. They appeared in the 1980's in the Bourne shell along with functions, but I've been unable to find any justification for them. – cdarke Jun 09 '15 at 10:25
  • @cdarke: The most straightforward and honest answer I've got so far. – Eugeniu Rosca Jun 09 '15 at 10:31
  • @Pynchia, your evaluation of these high-end programming languages does not help anyone ;) – Timo Aug 08 '20 at 06:18

2 Answers2

9

Parentheses are optional. From Bash Reference Manual --> 3.3 Shell Functions:

Functions are declared using this syntax:

name () compound-command [ redirections ]

or

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

This defines a shell 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 compound-command (see Compound Commands). That command is usually a list enclosed between { and }, but may be any compound command listed above. compound-command is executed whenever name is specified as the name of a command. When the shell is in POSIX mode (see Bash POSIX Mode), name may not be the same as one of the special builtins (see Special Builtins). Any redirections (see Redirections) associated with the shell function are performed when the function is executed.

So these are equivalent:

function hello {
    echo "hello there"
}

hello () {
    echo "hello there"
}

In Bash, functions can access global variables normally, so that the approach is slightly different from other languages. Normally, there is no need to use return because there is no value to catch.

See an example. Here, we have a global variable myvar containing a value. In the functions mytest and mytest_inner we are changing its value. However, in one case the value affects the global environment, whereas in the other does not.

In mytest we change the value and it affects the main block. In mytest_inner we do the same, but the value is just changed locally, in the sub-shell running in the function.

#!/bin/bash

function mytest {
   echo "mytest -> myvar: $myvar"
   ((myvar++))
}

function mytest_inner () {
   (
   echo "mytest_inner -> myvar: $myvar"
   ((myvar++))
   )
}

myvar=$1
mytest
echo "main -> myvar: $myvar"
mytest_inner
echo "main -> myvar: $myvar"

Let's run it:

$ ./myscript.sh 20
mytest -> myvar: 20
main -> myvar: 21
mytest_inner -> myvar: 21
main -> myvar: 21
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • I choose this answer, because of the referenced `Bash Reference Manual`. I do not see much connection between the question and the subshell-related explanations. Anyways, great effort. – Eugeniu Rosca Jun 12 '15 at 21:30
5

Why using round brackets in bash?

Actually, they're not needed, at least not in my version.

$ foo() { echo 'foo!' ; }

$ foo
foo!

$ function bar { echo 'bar!' ; }

$ bar
bar!

$ function baz() { echo 'baz!' ; }

$ baz
baz!

$ bash --version | head -n 1
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

man bash:

 Shell Function Definitions
   A shell function is an object that is called like a simple command and executes 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  compound-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  when-
          ever  name is specified as the name of a simple command.  Any redirections (see RE-
          DIRECTION below) specified when a function is defined are performed when the  func-
          tion 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  exe-
          cuted,  the  exit  status of a function is the exit status of the last command exe-
          cuted in the body.  (See FUNCTIONS below.)
ikegami
  • 367,544
  • 15
  • 269
  • 518