Writing Bash Functions

Summary: Using bash functions you can better structure your bash scripts and have finer-grained control over output redirection.

Function Structure

Let's start by examining the basic format of a bash function with the following example:

The sum function is declared with a name, two parentheses, and curly braces that wrap the function code body. Similar to regular bash commands/scripts it can be passed arguments, which are referenced positionally ($1, $2).

Output Redirect

Besides using functions to reduce code complexity and duplication, one of the main use cases for using a bash function would be to customize output redirection. Say you had a bash script where you had a function whose output didn't matter to the end user -  you could redirect the output of that particular function to /dev/null to silence its output while it performed its work. Here's an example:

Note the "2> /dev/null" after the closing curly brace - what this is saying is: when this function is invoked, redirect any standard error (2) to /dev/null, which effectively mutes the error that is raised when the "not_a_real_command" command is attempted to be run. A real-world application of that concept may be to alter which commands get run depending on how the user's environment has been set up (fall back to another command, etc.).

Return Values

By default, the return value of a bash function will be the value of the return value of the last command, and the return status will be the return status of the last command. To customize what the return value and status are you can use the "return" keyword:

Variable Scope

Lastly, function bodies can access global variables but can also declare their own variables local to the function scope. This can be achieved by using the "local" keyword as used in the previous code example.

Show Comments