更新时间:2022-12-14 19:35:12
限制内部函数的作用域
使用带括号()
定义的函数代替括号{}
:
Use function defined with parenthesis ()
instead of braces {}
:
f() (
g() {
echo G
}
g
)
# Ouputs `G`
f
# Command not found.
g
肢体功能在子shell中运行,它们具有与()
和{}
相同的语义,另请参见:
Parenthesis functions are run in sub-shells, which have the same semantics of ()
vs {}
, see also: Defining bash function body using parenthesis instead of braces
如果您想使用此功能,则无法使用
This cannot be used if you want to:
exit
cd
exit
cd
那些丢失在创建的子外壳中.
as those are lost in the created sub-shell.
另请参阅: bash函数:将身体括在括号中与括号