Bash Scripting | Functions Explained with Examples

Bash scripting can help users automate tasks, create powerful programs, and simplify their workflow. Functions are an important aspect of bash scripting, allowing code reuse and making the script more efficient.

In this article, the usage of function in bash scripting will be explained in detail with multiple examples.

  • How to Use Functions in Bash Scripting?
  • Simple Function
  • Function using a Single Argument
  • Function using Two Arguments
  • Function using a Local Variable
  • Function Using a Global Variable
  • Multiple Functions

How to Use Functions in Bash Scripting?

A bash function is a set of commands that can be called multiple times within a script or from the command line. Functions are defined using the ‘function’ keyword, followed by a name and a set of commands enclosed in curly braces {}. 

There are two syntaxes to define a function which are mentioned below:

Syntax 1: Here is an example of a simple bash function:

function_name() {
  # function code here
}

Syntax 2: There is another variation that you can apply to declare functions in bash which is mentioned below:

function function_name {
  # function code here
}

The above one is an old way to declare a function and is not recommended anymore as it is not compatible with other programming languages. Now, let’s discuss some examples of functions to explain their usage in more detail.

Example 1: Calling a Simple Function

In this example, a simple function has been created by writing the code mentioned below:

#!/bin/bash

# Define a function that echoes a greeting
function_name() {
  echo "Hello, Nice to meet You!"
}

# Call the function
function_name

Code Explanation:

  • An echo command has been used inside the main body to generate a simple message. 
  • This command will be executed when the “function_name” has been called.

A user can see the code output by running the command mentioned below, whereas the “function.sh” is the name of the bash script file.

$ bash function.sh

Example 2: Calling a Function Using a Single Argument

In this example, a single argument has been provided to the function by writing a code mentioned below:

function_name() {
  echo "Hello, $1! Welcome to Bash scripting."
}

# Call the function with a name
function_name "John"

Code Explanation:

  • A function has been defined that takes one argument and then calls that function with the name “John.”
  • Inside the main body of the function, a message will be printed starting with “Hello,” and then the argument will be passed to $1. 
  • This will print the argument value, which is “John” in this case, and then later, the remaining message will also be printed.

The output of the above code can be seen by executing the bash script below:

$ bash function.sh

Example 3: Calling a Function Using Two Arguments

In this example, two different arguments have been provided by writing a bash script mentioned below:

#!/bin/bash

add_numbers() {
  sum=$(( $1 + $2 ))
  echo "The sum of $1 and $2 is $sum"
}

# Call the function with two arguments
add_numbers 10 20

Code Explanation:

  • Two arguments have been provided, which will be received by the $1 and $2 when the main body is executed. 
  • After that, their sum will be calculated, and the result will be displayed using the echo command.

The output can be seen by executing the bash script below:

$ bash function.sh

Example 4: Calling a Function Using a Local Variable 

In this example, a local variable has been created by writing the code mentioned below:

#!/bin/bash

add_numbers ()  {
        read -p "Enter the first number:" num1
        read -p "Enter the second number:" num2
        local sum=$(( num1 + num2 ))
        echo "The sum of $num1 and $num2 is $sum"
}

add_numbers

Code Explanation:

  • Two numbers will be provided as Input.
  • Their sum will be calculated and stored inside a local variable “sum.” The result will be displayed using the echo command. 

The output of the above code can be seen after executing the bash script:

Example 5: Calling a Function Using a Global Variable

In this example, a global variable has been created by writing the code mentioned below:

#!/bin/bash
# define a global variable
my_var="hello"

# define a function that uses the global variable
my_function() {
  echo "The value of my_var is: $my_var"
}
# call the function
my_function

Code Explanation:

  • A global variable has been defined with the name my_var with the “hello” text. 
  • Now, when the my_function is called, the value of the my_var variable will be displayed using the echo command. 
  • As my_var is displayed outside of the function, which makes it a global variable and can be accessed by any function.

The output of the above code can be seen after executing the bash script:

$ bash function.sh

Example 6: Calling Multiple Functions

Multiple functions can also be created in the same code, and one of its examples is mentioned below:

#!/bin/bash

# Define the first function
function_1() {
  echo "First Function Output"
  echo "Hello, world!"
  echo "---------------------"
}

# Define the second function
function_2() {
  echo "Second Function Output"
  echo "Goodbye, world!"
}

# Call the functions
function_1
function_2

Code Explanation:

  • Two functions have been used with the name of function_1 and function_2
  • Calling function_1  and function_2 will execute the code inside their body respectively.

The output of the above code can be seen after executing the bash script:

$ bash function.sh

Conclusion

Using functions in a bash script is essential as it will simplify your code by breaking the large code into smaller portions. This will not only reduce the number of lines of the code but also save the user’s time. This article has provided multiple examples of the usage of functions in bash scripting.