Bash Functions in Linux


Introduction

Function is a section in the code where group of instructions are kept in one place. Like all other programming or scripting languages, we can use function in bash in Linux. There are many advantages using function than without using function. Some benefits are like, it is easy to read or execute the code and functions can be reused in code whenever needed.

Though there are some limitations in bash function, still it is good to use function for bash scripting if there is a need. There are two ways a bash function can be defined

  • Define bash function inside a shell script before the function is called.

  • Define a bash function directly in terminal command and call from terminal.

Let us see more details in this article.

Function Syntax

Here are the different ways we can define a bash function in Linux.

  • Use reserved keyword function and then name of the function.

function <function name> {
   <function body…>
}
  • Same like scenario 1 but it can be one line. Here semicolon [;] is required at the end of command.

function <function name> { <function body…> ; }
  • No need of function keyword, just use the function name.

<function name> {
   <function body…>
}
  • Same like scenatio 3 but it can be one line. Here also semicolon [;] is required at the end of command.

<function name> { <function body…> ; }

Now let us try some bash programming to understand bash function.

Approach 1: 4 types of function definition

Example

#!/bin/bash

# One line
hello1 () { echo "I am in hello1" ; }

# One line but declaring functions using the reserved word function
function hello2 { echo "I am in hello2"; }


# Multiline
function hello3 {
   echo "I am in hello3" ;
}

# Declaring functions without the function reserved word
# Multiline
hello4 () {
   echo "I am in hello4" ;
}
# Invoking functions
hello1
hello2
hello3
hello4

In this program we have defined four functions [hello1, hello2, hello3, hello4] using different styles. All methods are correct and can be used as per user’s need.

To run a bash script, It is good to give execute permission for file name using below command.

chmod 777 <bash script file name>

Then run the bash script like

./<filename.sh>

Output

I am in hello1
I am in hello2
I am in hello3
I am in hello4

Approach 2: Get the return value of bash function using “$?”

Example

#!/bin/bash

bash_return () {
   echo "I want to return 100"
   return 100
}
bash_return
echo $?

Here function “bash_return” 100 and we get it via “$?” variable. However, this is not a good approach to get the return value from function. Because this return exits from code.

Output

I want to return 100
100

Approach 3: Get the return value of bash function using global variable

Example

#!/bin/bash

bash_return_using_global_var () {
   global_var="I am inside global variable"
}

bash_return_using_global_var
echo $global_var

In this program, we have used one global variable to get the retrun value from function.

Output

I am inside global variable

Approach 4: Get the return value of bash function using stdout

Example

#!/bin/bash

bash_return_to_stdout () {
   local bash_return1="10000"
   echo "Inside function -> $bash_return1"
}

bash_return1="$(bash_return_to_stdout)"
echo "$bash_return1"

In this program, we used local variable to store the function return value. This value can be reused later for any other purpose.

Output

Inside function -> 10000

Approach 5: To PASS arguments to bash function

Example

#!/bin/bash

pass_arg () {
   echo "Here is the argument passed to this function: $1"
}

pass_arg "TUTORIALSPOINT"

Here we have passed first argument to function.

Output

Here is the argument passed to this function: TUTORIALSPOINT

Approach 6: Declare bash function in terminal directly and call from terminal

Here we need to open a terminal and define a function like below. And press enter.

Example

terminal_func () { echo "I am running from terminal"; echo "Thank You"; }

Then type the function name “terminal_func” in terminal and press enter.

Output

I am running from terminal
Thank You

If we want to delete a bash terminal function then we can use below command in terminal

unset terminal_func

Then if we try to call function “terminal_func” , we will get below error

terminal_func: command not found

Conclusion

From this article, we have to learn many aspect of bash function like defining a function in different ways, return value of function, pass function argument, run bash function from terminal. This helps us to write easy and efficient bash scripting in Linux.

Updated on: 29-Mar-2023

645 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements