如果bash函数始终未填充参数,为什么要使用圆括号呢? [英] Why bash functions use round brackets, if those are never filled with arguments?

查看:66
本文介绍了如果bash函数始终未填充参数,为什么要使用圆括号呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The function definition syntax for various languages is:

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中使用圆括号?

推荐答案

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

使用以下语法声明函数:

Functions are declared using this syntax:

name () compound-command [ redirections ]

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

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

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.

所以这些是等效的:

function hello {
    echo "hello there"
}

hello () {
    echo "hello there"
}

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

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.

查看示例.在这里,我们有一个包含值的全局变量 myvar .在函数 mytest mytest_inner 中,我们正在更改其值.但是,在一种情况下,该值会影响全球环境,而在另一种情况下,则不会.

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.

mytest 中,我们更改该值,它会影响主块.在 mytest_inner 中,我们执行相同的操作,但是该值只是在函数中运行的子外壳中本地更改.

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"

让我们运行它:

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

这篇关于如果bash函数始终未填充参数,为什么要使用圆括号呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆