What Is Bash Function In Linux How To Use It

What are Bash Functions in Linux?

Bash functions are reusable code blocks in a bash script that can be grouped together and named to avoid writing the same code repeatedly. Much like other programming languages, you get the option to use functions in your bash scripts. Functions help you organize your entire script into smaller modules, making your code more readable, easy to debug, and reusable. In other words, functions are like a script within a script.

How to Define Functions in Bash?

Now that you have a basic idea of what are bash functions, let’s see how to define them to make our Linux workflow much easier. The basic syntax to define bash functions are:
You can even use the single-line version of the above syntax:
Some points to remember about defining bash functions include:

Every statement and command which are written inside the {} is known as the function body. For the function name, you can use anything except a keyword which are specific words reserved for different commands. Unlike conventional bash script, functions do not get executed unless you call them. To execute a function, you need to use the function name. When using the single-line version, every command you write must be separated with a semi-colon ‘;’. All variables declared inside a function can only be accessed inside the body of the function.

Let’s see a basic example based on the above syntax. Start by creating a new file in Linux text editors and write this code inside it:
It is recommended to save the file with the same name as the function name. To invoke the file, simply write the function name inside the shell prompt and hit enter.

How to Pass Arguments to Bash Functions

Sometimes you may need to work with some custom values or work with the user input values with the functions you’ve created. To pass arguments to the bash function, simply mention them after the function name after invoking the function as Linux shell variables. As for using the arguments inside the function, place them with $<argument_position>, like $1, $2 below and so on. For example:
To invoke, use the following command:

How to Return Value from Bash Functions

Unlike other programming languages, in bash, you cannot return values from the function. You can only send the return status from the bash function to the caller with any number ranging from 0 to 255, where 0 represents success and any other number as a Linux system error/failure. For example, copy this code in the nano editor in Linux. When you call the above script, you will get a similar output along with the value 10, indicating a failure or error has occurred.

Variable Scope in Bash Functions

The scope of a variable means code parts where the variables can be used in bash. The scope can be divided into mainly two parts – global and local scope. With the global scope, the variable can be used everywhere including directly in the shell prompt. Local scope means the variable cannot be used outside the block where it is declared.
Every variable you declare inside a bash function with the “local” keyword will fall under the local scope of the function body and cannot be used outside of the function body. Similarly, global variables can be accessed from anywhere in the bash script without any restrictions. The following example will make it clear:
After executing the above script, you will see the following output:

Best Practices to Use Functions in Bash

Bash functions are powerful tools that can enhance the structure, maintainability, and reusability of bash scripts in Linux. Follow these best practices to use functions in bash that can vastly improve our workflow: