加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用括号而不是大括号定义bash函数体

发布时间:2020-12-15 18:57:29 所属栏目:安全 来源:网络整理
导读:该脚本演示了如何使用括号定义bash函数 带括号的经文.括号具有很好的制作效果 我想,在函数“local”中创建的环境变量 因为函数体是作为子shell执行的.输出是: A=somethingA=B=somethingB=something 问题是这是否允许定义函数的语法. #!/bin/bashfoo() ( exp
该脚本演示了如何使用括号定义bash函数
带括号的经文.括号具有很好的制作效果
我想,在函数“local”中创建的环境变量
因为函数体是作为子shell执行的.输出是:
A=something
A=
B=something
B=something

问题是这是否允许定义函数的语法.

#!/bin/bash

foo() (
    export A=something
    echo A=$A
)

bar() {
    export B=something
    echo B=$B
}

foo
echo A=$A
bar
echo B=$B
是的,允许使用该语法.如 bash man page中所述,bash函数的定义是:
[ function ] name () compound-command [redirection]

更多描述(也来自手册页):

The body of the function is the compound command compound-command. That command is usually a list of commands between { and },but may be any command listed under Compound Commands above.

()和{}括号列表是复合命令.完整列表(再次从手册页,只是编辑到一个简单的列表):

A compound command is one of the following:

06001

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读