为什么bash函数使用圆括号,如果它们从不填充参数?

Eug*_*sca 3 bash

各种语言的函数定义语法是:

C(所有脚本语言的教父):

func_type myfunc_c (arg_type arg_name , ...)
{
    /* arguments explicitly specified */
}
Run Code Online (Sandbox Code Playgroud)

TCL:

proc myfunc_tcl {arg1 arg2 args} {
    # arguments explicitly specified
}
Run Code Online (Sandbox Code Playgroud)

Perl的:

sub myfunc_perl {
    # no arguments explicitly specified && no round brackets used
}
Run Code Online (Sandbox Code Playgroud)

蟒蛇:

def myfunc_python(arg1, arg2):
    # arguments explicitly specified
Run Code Online (Sandbox Code Playgroud)

击:

function myfunc_bash () {
    # arguments NEVER explicitly specified
    # WHY using round brackets?
}
Run Code Online (Sandbox Code Playgroud)

为什么在bash中使用圆括号?

fed*_*qui 9

括号是可选的.从Bash参考手册 - > 3.3 Shell函数:

使用以下语法声明函数:

name () compound-command [ redirections ]
Run Code Online (Sandbox Code Playgroud)

要么

function name [()] compound-command [ redirections ]
Run Code Online (Sandbox Code Playgroud)

这定义了一个名为name的shell函数.保留字功能是可选的.如果提供了function保留字,则括号是可选的.函数体是复合命令compound-command(参见Compound命令).该命令通常是{和}之间的列表,但可以是上面列出的任何复合命令.只要将name指定为命令的名称,就会执行compound-command.当shell处于POSIX模式时(参见Bash POSIX模式),名称可能与其中一个特殊内置函数不同(参见Special Builtins).执行该函数时,将执行与shell函数关联的任何重定向(请参阅重定向).

所以这些是等价的:

function hello {
    echo "hello there"
}

hello () {
    echo "hello there"
}
Run Code Online (Sandbox Code Playgroud)

在Bash中,函数可以正常访问全局变量,因此该方法与其他语言略有不同.通常,没有必要使用,return因为没有值可以捕获.

看一个例子.在这里,我们有一个myvar包含值的全局变量.在功能mytestmytest_inner我们正在改变其价值.但是,在一种情况下,该值会影响全球环境,而另一种情况则不会.

mytest我们更改值时它会影响主块.在mytest_inner我们做同样的事情,但是在函数中运行的子shell中,值只是在本地更改.

#!/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"
Run Code Online (Sandbox Code Playgroud)

我们来吧:

$ ./myscript.sh 20
mytest -> myvar: 20
main -> myvar: 21
mytest_inner -> myvar: 21
main -> myvar: 21
Run Code Online (Sandbox Code Playgroud)


ike*_*ami 5

为什么在bash中使用圆括号?

实际上,它们并不需要,至少在我的版本中没有.

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

$ foo
foo!

$ function bar { echo 'bar!' ; }

$ bar
bar!

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

$ baz
baz!
Run Code Online (Sandbox Code Playgroud)

$ bash --version | head -n 1
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
Run Code Online (Sandbox Code Playgroud)

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.)
Run Code Online (Sandbox Code Playgroud)