什么是在一些的bash脚本中使用的“功能”关键字? [英] What is the 'function' keyword used in some bash scripts?
问题描述
function foo() {}
我做出信息庆典搜索查询
,并期待在POSIX的releted章节的功能发现关键字,但一无所获。
I make search queries in info bash
and look in releted chapters of POSIX for function keyword but nothing found.
什么是在一些的bash脚本使用功能
关键字?就是一些德precated语法?
What is function
keyword used in some bash scripts? Is that some deprecated syntax?
推荐答案
的函数
定义中的Bash一种功能,当关键字是可选的,如在的manual :
The function
keyword is optional when defining a function in Bash, as documented in the manual:
功能都采用这种语法宣布:
Functions are declared using this syntax:
名称()复合命令[重定向]
或
函数名[()]复合命令[重定向]
语法的第一种形式通常preferred,因为它与伯恩/科恩/ POSIX兼容的脚本,因此更加便于携带。结果
这就是说,有时你可能需要使用函数
关键字prevent猛砸的别名与您的函数名碰撞的。考虑这个例子:
The first form of the syntax is generally preferred because it's compatible with Bourne/Korn/POSIX scripts and so more portable.
That said, sometimes you might want to use the function
keyword to prevent Bash aliases from colliding with your function's name. Consider this example:
$ alias foo="echo hi"
$ foo() { :; }
bash: syntax error near unexpected token `('
在这里,'富'
是由同名的别名的文本替换,因为它的的第一个字的命令。随着函数
别名未展开:
Here, 'foo'
is replaced by the text of the alias of the same name because it's the first word of the command. With function
the alias is not expanded:
$ function foo() { :; }
这篇关于什么是在一些的bash脚本中使用的“功能”关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!