什么是一个bash shell脚本函数定义用于像&QUOT括号; F(){}"?它比使用&QUOT不同,功能"关键词? [英] What are the parentheses used for in a bash shell script function definition like "f () {}"? Is it different than using the "function" keyword?

查看:137
本文介绍了什么是一个bash shell脚本函数定义用于像&QUOT括号; F(){}"?它比使用&QUOT不同,功能"关键词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我心中已经一直想知道他们是用什么呢?
看来愚蠢到把他们在每一个时间,如果你不能把任何东西在他们里面。

 函数名(){
    #statements
}

另外还有什么赢得/带把函数关键字在函数的开始输?

 功能函数名(){
    #statements
}


解决方案

关键字函数已去$ P $赞成pcated FUNCTION_NAME ()为与<可移植性href=\"http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_05\">POSIX规范


  

一个功能是用户定义的名称
  作为一个简单的命令调用
  新的位置复合命令
  参数。函数与定义
  一个函数定义的命令。


  
  

函数定义的格式
  命令如下:

  FNAME()复合命令[IO-重定向...]


请注意, {} 不是强制性的,所以如果你不打算使用关键字函数(你不应该),那么()是必要的,这样解析器知道你定义的函数。

例如,这是一个法律的功能定义和调用:

  $ MYFUNC()给arg;不要回声$ ARG完成的; MYFUNC富巴

酒吧

I'v always wondered what they're used for? Seems silly to put them in every time if you can never put anything inside them.

function_name () {
    #statements
}

Also is there anything to gain/lose with putting the function keyword at the start of a function?

function function_name () {
    #statements
}

解决方案

The keyword function has been deprecated in favor of function_name() for portability with the POSIX spec

A function is a user-defined name that is used as a simple command to call a compound command with new positional parameters. A function is defined with a "function definition command".

The format of a function definition command is as follows:

fname() compound-command[io-redirect ...]

Note that the { } are not mandatory so if you're not going to use the keyword function (and you shouldn't) then the () are necessary so the parser knows you're defining a function.

Example, this is a legal function definition and invocation:

$ myfunc() for arg; do echo "$arg"; done; myfunc foo bar
foo
bar

这篇关于什么是一个bash shell脚本函数定义用于像&QUOT括号; F(){}&QUOT;?它比使用&QUOT不同,功能&QUOT;关键词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆