Why cannot I define an empty function in shell?

后端 未结 2 785
猫巷女王i
猫巷女王i 2021-01-12 03:56

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

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

bas         


        
相关标签:
2条回答
  • 2021-01-12 04:15

    Try this instead:

    empty_func() {
      :
    }
    
    0 讨论(0)
  • 2021-01-12 04:26

    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 can't be empty. The closest you can get is to use a null statement or return:

    function empty_func() {
        : 
    }
    

    or

    function empty_func() {
        return
    }
    
    0 讨论(0)
提交回复
热议问题