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

前端之家收集整理的这篇文章主要介绍了使用括号而不是大括号定义bash函数体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
该脚本演示了如何使用括号定义bash函数
带括号的经文.括号具有很好的制作效果
我想,在函数“local”中创建的环境变量
因为函数体是作为子shell执行的.输出是: @H_301_4@A=something A= B=something B=something

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

@H_301_4@#!/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函数的定义是: @H_301_4@[ 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

猜你在找的Bash相关文章