为什么我不能在shell中定义一个空函数?

mor*_*ora 7 bash shell

我在学习bash.我不小心遇到了空函数的语法错误.

#!/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: `}'
Run Code Online (Sandbox Code Playgroud)

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

usr*_*usr 11

bash shell中的语法根本不允许空函数.函数的语法是:

  name () compound-command [redirection]
  function name [()] compound-command [redirection]
Run Code Online (Sandbox Code Playgroud)

并在以下形式的复合命令中:

{ list; }
Run Code Online (Sandbox Code Playgroud)

list不能为空.你可以得到的最接近的是使用null语句或返回:

function empty_func() {
    : 
}
Run Code Online (Sandbox Code Playgroud)

要么

function empty_func() {
    return
}
Run Code Online (Sandbox Code Playgroud)


red*_*neb 5

试试这个:

empty_func() {
  :
}
Run Code Online (Sandbox Code Playgroud)