Why do bash functions use parentheses if they are never filled with arguments?

The syntax for defining functions for different languages ​​is:

C (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 use parentheses in bash?

+3


source to share


2 answers


The 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 wrapper function named name. The reserved word function is optional. If a reserved word is reserved function

, the parentheses are optional
. The body of a function is a compound command compound command (see compound commands). This command is usually a list enclosed between {and}, but can be any combination of the command above. a compound command is executed whenever a name is specified as a command name. When the shell is in POSIX mode (see Bash POSIX mode), the name can be different from one of the special names (see special built-in devices). Any redirects (see the Redirects section) associated with a wrapper function are executed when the function is executed.

So they are equivalent:

function hello {
    echo "hello there"
}

hello () {
    echo "hello there"
}

      

In Bash, functions can access globals just fine, so the approach is slightly different from other languages. Usually not necessary to use return

because there is no value for catch.



See example. Here we have a global variable myvar

containing the value. In functions mytest

and mytest_inner

we change its value. However, in one case, the value affects the global environment, while in the other it does not.

In mytest

we change the value, and it affects the main block. In mytest_inner

we do the same thing, but the value is just changed locally, in the sub-wrapper 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

      

+8


source


Why use parentheses in bash?

Actually, they are 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.)

      

+5


source







All Articles