微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

该脚本演示了如何使用括号定义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

原文地址:https://www.jb51.cc/bash/386888.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐