Blog Post
What You Need to Know about the Bash Function

What You Need to Know about the Bash Function

One of the available functions in a Linux-based operating system is the bash function. In its essence, this function is a chain of commands a user can call multiple times. Although it does come with certain limitations, bash can also improve the readability of your bash scripts and save you from writing repetitive lines of code.

Let’s take a look at the basics of the bash function and how you can utilize it.

Basic Syntax

The basic syntax of the bash function looks like this:

function preferred_name () { commands; }

Instead of “commands,” you should type the desired list of commands you want the function to execute. That section is also known as the function’s body. As a side note, you can use our basic commands list inside this function.

Here are a couple of things to note:

  • You can use both single line and multiple line options, but keep in mind that the command list always needs to be between the braces. Additionally, make sure to use newlines or spaces to separate the braces from the command list.
  • The single-line option must contain a “;” after the final command in the list.
  • You can execute the function by calling it while in the shell script. The fact you created a function doesn’t imply its automatic execution.
  • You can also define a function without writing the “function” prefix. However, make sure you got all the other parts right.

We would advise you to take advantage of the descriptive words when creating function names. That will be a lot easier to know the difference between different functions than using, for example, only numbers.

Bash Function Example

We want you to understand everything right, so here is an example of a bash function:

my_function () {
echo 'hi, guys'

As you can see, we named it “my_function” and used multiple lines to make room for several commands. We only used one, but if you need to add more, just insert new lines in the middle.

Remember to use the curly bracket to finish the function.

When you want to execute the function, type:


The output provided will be displayed in the form of “hi, guys.” as you can see below:

bash function example

Bash Function Variables

The first thing to note are global variables. You can access them regardless of where you are in the script. Keep in mind that the bash function automatically defines variables as global, and that applies to those variables executed within a function.

You need to use the “local” prefix to set a local variable. That way you can only use it within that particular function. Each function is separate, which means you can set local variables that will have the same name in every one of them.

Here is an example:

function_name () {
local var 2='M'
echo "Inside function: var1: $var1, var2: $var2"

The first thing we did was to define two global variables. Once we started the function, however, we changed the global variable 1. We then moved on to defining a local variable 2. Inside this function, var2 will be “M” and var1 will be ‘Y,’ but once you execute the function, var1 will be ‘Y’ and var2 will be ‘Z.’

The conclusion is that you can use any function to adjust the global variables. Also, when it comes to using local variables within the function, they will have a priority over the global variables.

Return Values

The important thing to note is that when you call a bash function, you won’t be able to return a value. The return value is the last statement performed in the bash function. In case of success, the value will be “0,” but in case of failure, the range of return values may vary from 1 to 255.

Let’s take a look at how to specify the return status.

function_name () {
echo "function outcome"
return 33
echo $?

It is interesting to note that this statement can terminate the function. That is why many consider it the exit status of a function.

You can also attach function result to a global variable:

function_name () {
func_result="function outcome"
return 33
echo $func_result

bash function variables with return values

Bash Function Passing Argument

The last thing we will show you in this tutorial is how to pass arguments to this function. It is quite simple as all you need to do is to ensure they follow the name of the function:

function_name () {
echo "Hi $n"

If you replace “$n” with “$1,” the result will be something like:

Hi Tom

There are several things to keep in mind with passing arguments:

  • $n should be replaced with the desired parameter position. In our case, Tom is the parameter corresponding to “$1.”
  • If you use the $0, you will notice that it will print the name of the function.

bash function passing argument

Wrap Up

That rounds up our guide on what you need to know about the bash function. Although it is not considered an advanced function, it can still be useful and save you some time. Go ahead and try it – we are certain you will get ahold of it quickly. Feel free to ask any questions if something is unclear on the topic.

Related posts

Leave a Reply

Required fields are marked *

Copyright © 2022 All rights reserved.