为什么不能在Shell中定义空函数? [英] Why cannot I define an empty function in shell?

查看:566
本文介绍了为什么不能在Shell中定义空函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习bash.我不小心遇到了一个语法错误,函数为空.

I am learning bash. I accidentally encounter a syntax error with empty function.

#!/bin/bash
# script name : empty_function.sh
function empty_func() {
}

bash empty_function.sh

empty_function.sh: line 3: syntax error near unexpected token `}'
empty_function.sh: line 3: `}'

我想是因为定义了一个空函数. 我想知道为什么我不能定义一个空函数?

I suppose it is because of definition of an empty function. I would like to know Why I cannot define an empty function?

推荐答案

The bash shell's grammar simply doesn't allow empty functions. A function's grammar is:

  name () compound-command [redirection]
  function name [()] compound-command [redirection]

并采用以下形式的复合命令:

And in a compound command of the form:

{ list; }

list不能为空.您可以获得的最接近的结果是使用null语句或返回:

list can't be empty. The closest you can get is to use a null statement or return:

function empty_func() {
    : 
}

function empty_func() {
    return
}

这篇关于为什么不能在Shell中定义空函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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