Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cannot I define an empty function in shell?

Tags:

bash

shell

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?

like image 277
mora Avatar asked Sep 03 '16 13:09

mora


People also ask

How do you declare an empty variable in shell script?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

What does $0 mean in shell script?

$0 Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands (see Section 3.8 [Shell Scripts], page 39), $0 is set to the name of that file.

How do you create an empty array in bash?

To declare an empty array, the simplest method is given here. It contains the keyword “declare” following a constant “-a” and the array name. The name of the array is assigned with empty parenthesis.

What is an empty function called?

A "noop" or "null function": var noop = function(){}

How to use a function in a shell script?

Keep the function in the same script where it is used. Write a library of useful functions inside a file and import the file in the script where it is required. A function can be used to perform various operations in a shell script.

Can a shell function return a value?

A shell function may do neither, either or both. It is generally accepted that in shell scripts they are called functions. A function may return a value in one of four different ways: Change the state of a variable or variables

What is the difference between a procedure and a shell function?

A procedure, on the other hand, does not return a value, but may produce output. A shell function may do neither, either or both. It is generally accepted that in shell scripts they are called functions. A function may return a value in one of four different ways:

What happens when you declare a variable outside of a function?

An important thing to note here is that if you declare a variable outside a function, it will behave like a global variable and can be accessed inside a function. 4. Functions that echo output to the standard output.


2 Answers

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
}
like image 89
P.P Avatar answered Oct 27 '22 13:10

P.P


Try this instead:

empty_func() {
  :
}
like image 38
redneb Avatar answered Oct 27 '22 14:10

redneb