12

Is it possible to call a function which is declared below in bash?

Example

if [ "$input" = "yes" ]; then
    YES_FUNCTION
elif [ "$input" = "no" ]; then
    NO_FUNCTION
else
    exit 0;
fi

YES_FUNCTION()
{
  .....
  .....
}

NO_FUNCTION()
{
  .....
  .....
}
32

Like others have said, you can't do that.

But if you want to arrange the code into one file so that the main program is at the top of the file, and other functions are defined below, you can do it by having a separate main function.

E.g.

#!/bin/sh

main() {
    if [ "$1" = yes ]; then
        do_task_this
    else
        do_task_that
    fi
}

do_task_this() {
    ...
} 
do_task_that() {
    ...
} 

main "$@"; exit

When we call main at the end of file, all functions are already defined. Explicitly passing "$@" to main is required to make the command line arguments of the script visible in the function.

The explicit exit on the same line as the call to main is not mandatory, but can be used to prevent a running script from getting messed up if the script file is modified. Without it, the shell would try to continue reading commands from the script file after main returns. (see How to read the whole shell script before executing it?)

  • @ikkachu think it should work.. let me check. – msp9011 Jun 13 '18 at 13:10
  • 7
    With Bash scripts I often use [[ ${BASH_SOURCE[0]} = "$0" ]] && Main "$@" to call the main function so I can source it in another script without Main being executed. Then I can either reuse the functions or write tests to check them. – BlackJack Jun 13 '18 at 14:10
  • 11
    Having main "$@"; exit (with exit on the same line as main) is also useful as a safeguard against the file being modified while it is interpreted. – Stéphane Chazelas Jun 13 '18 at 15:03
  • 2
    @JoL, what is read is not read again, and the shell will need to read and parse the whole text of a loop before starting to run it, but then after the loop returns it will carry on reading from the rest of the file at the current position (and if the file has been modified it will screw things up). If everything is in functions, the shell needs to read everything before starting to do anything (except defining those functions), if we put the exit on the same line as main we make sure the shell won't read anything again from the file after main returns. – Stéphane Chazelas Jun 13 '18 at 15:45
  • 1
    @MontyHarder, it doesn't matter if you use main; exit, main; exit $? or main <EOF>, in all cases the exit code of main is used as the exit code of the script. The exit would be just to prevent things getting messed up if someone edits the script while it's running. – ilkkachu Jun 13 '18 at 16:36
11

No, the functions have to exist in the shells environment at the time of calling them.

Google's "Shell Style Guide" has a fix for this:

A function called main is required for scripts long enough to contain at least one other function.

At the very end of the script, after all functions, as the only statement not in a function, you would have

main "$@"

This would call the main function with whatever parameters the script was given. The main function could be located at the top of the script (the style guide says to put it at the bottom, but then again, it says many things).

When the shell gets to the main call, all functions in the script have been parsed and can therefore be called from within the main function.

9

No, functions have to be declared before they’re used. Shell scripts are read line by line and acted upon line by line; so a function doesn’t exist until its declaration has been executed.

  • your are right. the problem is I have 30+ functions in a script. it's quite tricky when we read the code. In C it's comfortable. – msp9011 Jun 13 '18 at 10:22
  • 3
    You can put your function declarations in another file and source it (. yourfile). – Stephen Kitt Jun 13 '18 at 10:24
  • Yes, I have tried that, but the requirement is to have a single script. – msp9011 Jun 13 '18 at 10:26
  • @SivaPrasath What's exactly the problem? Just define all the functions, maybe even putting the main code in a function and then the last line shows which function is called and contains the main part of the script. – BlackJack Jun 13 '18 at 13:58
  • @SivaPrasath In C, you don't have bare if statements outside of a function. The function doesn't have to be defined when you declare the if-containing function, just when you call it. – chepner Jun 13 '18 at 14:12
4

The shell has no concept of declaring a function. So you cannot have a forward declaration.

As a consequence, you need to have the function implementation read by the shell before it can be called.

  • 4
    Technically, some shells (ksh, zsh) have a function autoloading feature which could be seen as some form of declaration (where autoload f declares the function, but its body is only loaded upon the first invocation). That doesn't apply to the OP's bash though. – Stéphane Chazelas Jun 13 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.