【问题标题】:Why bash functions use round brackets, if those are never filled with arguments?为什么 bash 函数使用圆括号,如果它们从未填充过参数?
【发布时间】:2015-06-08 15:06:51
【问题描述】:

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

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

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

重击:

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

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

【问题讨论】:

  • 你对 C 语言的描述会让很多人感到不安.. :)
  • 它在哪里说你必须在 bash 中有括号? Bash HOWTO doesn't use them..
  • @Pynchia:我添加了 [c] 标签来抵消反对票! :) 我不会放弃我的信仰!! :D Martijn Pieters && ThisSuitIsBlackNot:感谢您提供宝贵的信息,伙计们!
  • Posix shell 需要 () 但 bash 允许使用 function 关键字。两者都不需要 {}。 (主体需要是一个复合命令。试试这个,例如:f() for a; do echo Argument "$a"; done
  • 如果您问为什么 POSIX 标准需要它们而 Bash 不需要它们,尤其是当它们似乎没有功能时,这可能会更好。它们与函数一起出现在 1980 年代的 Bourne shell 中,但我一直无法为它们找到任何理由。

标签: bash


【解决方案1】:

括号是可选的。来自Bash Reference Manual --> 3.3 Shell Functions

函数使用以下语法声明:

name () compound-command [ redirections ]

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

这定义了一个名为 name 的 shell 函数。保留字功能 是可选的。 如果提供了function 保留字,则 括号是可选的。函数的主体是复合 command 复合命令(请参阅复合命令)。该命令是 通常是包含在 { 和 } 之间的列表,但可以是任何复合词 上面列出的命令。每当名称为时执行复合命令 指定为命令的名称。当 shell 处于 POSIX 模式时 (请参阅 Bash POSIX Mode),名称可能与特殊之一不同 内置(参见特殊内置)。任何重定向(请参阅重定向) 与 shell 函数相关联的函数在执行时执行 执行。

所以这些是等价的:

function hello {
    echo "hello there"
}

hello () {
    echo "hello there"
}

在 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"

让我们运行它:

$ ./myscript.sh 20
mytest -> myvar: 20
main -> myvar: 21
mytest_inner -> myvar: 21
main -> myvar: 21

【讨论】:

  • 我选择这个答案,因为引用了Bash Reference Manual。我看不出这个问题和与 subshel​​l 相关的解释之间有什么联系。无论如何,非常努力。
【解决方案2】:

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

实际上,它们不需要,至少在我的版本中不需要。

$ 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.)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2013-06-16
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    相关资源
    最近更新 更多