Understanding functions in Linux bash script

April 23, 2021

We human beings, in our day-to-day lives, are helped by people who have certain knowledge or skills, such as doctors, lawyers, and barbers. This helps our lives to be more organized and comfortable so that we need not learn every skill in this world. We take advantage of skills that have already been acquired by other people. The same thing applies to software development as well. If we use code or scripts that have already been developed, this will save our time and energy.

In real-world scripts, we break down big tasks or scripts into smaller logical tasks. This modularization of scripts helps in the better development and understanding of code. Functions can be called the smaller logical blocks inside the shell script.

The advantages of functions are as follows:

  • If the script is very big, then understanding it becomes very difficult. Using functions, we can easily understand complex script through logical blocks or functions.
  • When a big and complex script is divided into functions, then it becomes easy to develop and test the script.
  • If a certain part of code is repeated again and again in the big script, then using functions to replace repetitive code is very practical, such as checking whether the file or directory is present or not.
  • We define functions for specific tasks or activities. Such functions can be called as commands in scripts.

Functions can be defined on a command line or inside scripts. The syntax for defining functions on a command line is as follows:

functionName { command_1; command_2; . . . }

We could also use this:

functionName() { command_1; command_2; . . }

In single-line functions, every command should end with a semicolon.

Let’s write a very simple function to illustrate the preceding syntax:

$ hello() { echo 'Hello world!';}

We can use the previously defined function as follows:

$ hello

This should produce the following output:


Hello world!

The syntax of the function declaration inside the shell script is as follows:

function_name() { 
   block of code 

An alternate function syntax is mentioned here:

function function_name 
   block of code 

Functions should be defined at the beginning of a script.

We can add this function in the shell script function_01.sh as follows:


   echo "Executing function hello" 
echo "Script has started now" 
echo "Script will end" 

Test the script as follows:

$ chmod +x function_01.sh
$ ./function_01.sh

This should produce the following output:


Script has started nowExecuting function helloScript will end

We can modify the preceding script and create function_02.sh with some more functionality, shown as follows:


function greet() 
{ echo "Hello $LOGNAME, today is $(date)"; } 

Test the script as follows:

$ chmod +x function_02.sh
$ ./function_02.sh

This should produce the following output:


Hello ganesh, today is Sun Jul 5 22:47:23 PDT 2015

The system init functions are placed in the /lib/lsb/init-functions folder in the Linux operating system:

The script function_03.sh has a function for listing the present working directory and listing all the files in the current directory, as follows:


function_lister () 
   echo "Your present working directory is `pwd`" 
   echo "Your files are:" 

Test the script as follows:

$ chmod +x function_03.sh
$ ./function_03.sh

This should produce the following output:


Your present working directory is /home/student/Desktop/testYour files are:01.sh  02.sh  03.sh

The script function_04.sh with a function to pause the script until users press any key is as follows:


# pause: causes a script to take a break 
echo "To continue, hit RETURN." 
read q 

Test the script as follows:

$ chmod +x function_04.sh$ ./function_04.sh



To continue, hit  RETURN.(after hitting any key it resumes)

The script function_05.sh with a function to print the previous day is as follows:


date --date='1 day ago' 

Test the script as follows:

$ chmod +x function_05.sh
$ ./function_05.sh

This should produce the following output:


Sat Jul  4 22:52:24 PDT 2015

The function to convert lowercase letters into uppercase letters is shown in function_06.sh as follows:


function convert_upper() 
echo $1 | tr 'abcdefghijklmnopqrstuvwxyz'  
convert_upper "ganesh naik - embedded android and linux training"

Test the script as follows:

$ chmod +x function_06.sh
$ ./function_06.sh

This should produce the following output:



Displaying functions

If you want to see all the declared functions in the shell environment, then enter the following command:

$ declare -f

If you want to see a particular function, then here is the command:

$ declare -f hello

This should produce the following output:


hello (){    echo 'Hello world!'}

Removing functions

If we no longer need the function in the shell, then we use the following command:

$ unset -f hello$ declare -f hello          # Check the function in shell environment.

Nothing will be displayed on the screen, as the hello function is removed from the shell environment with the unset command.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur


Submit a Comment

Your email address will not be published. Required fields are marked *

two × three =