Passing arguments or parameters to functions using Linux bash script

April 24, 2021

In certain situations, we may need to pass arguments or parameters to functions. In such situations, we can pass arguments as follows.

Calling the script with command-line parameters is as follows:

$ name arg1 arg2 arg3 . . .

Let’s type a function as follows:

$  hello() { echo "Hello $1, let us be a friend."; }

Call the function in the command line as follows:

$ hello Ganesh

This should produce the following output:

Output:

Hello Ganesh, let us be a friend

Let’s write the script function_07.sh. In this script, we pass command-line parameters to the script as well as the function:

function_07.sh

#!/bin/bash 
quit() 
{ 
         exit 
} 
ex() 
{ 
    echo $1 $2 $3 
} 
ex Hello hai bye     # Function ex with three arguments 
ex World             # Function ex with one argument 
echo $1        # First argument passed to script 
echo $2        # Second argument passed to script 
echo $3        # Third argument passed to script 
quit 
echo foo 

Test the script as follows:

$ chmod +x function_07.sh
$ ./function_07.sh One Two Three

This should produce the following output:

Output:

Hello hi byeWorldOneTwoThree

We can observe from the output that the parameters passed to the function are local to the function. In global scope, the command-line parameters to the script are available as $1, $2, $3, and more.

Another example script, called function_08.sh, to pass multiple arguments to the function is as follows:

function_08.sh

#! /bin/bash 
folder=~/Desktop/abc 
cdate=$(date +"%Y-%m-%d-%H:%M") 
inotifywait -m -q -e create -r --format '%:e %w%f' $folder | while read file 
do 
	mv ~/Desktop/abc/output.txt ~/Desktop/Old_abc/${cdate}-output.txt 
done

Test the script as follows:

$ chmod +x function_08.sh
$ ./function_08.sh

This should produce the following output:

Output:

Calling countries() for first timecountries(): $0 = ./hello.shcountries(): $1 = USAcountries(): total number of args passed = 1countries(): all arguments ($*) passed = -"USA"Calling countries() second timecountries(): $0 = ./hello.shcountries(): $1 = USAcountries(): total number of args passed = 3countries(): all arguments ($*) passed = -"USA India Japan"

We can create a function that could create a new directory and change to it during the execution of the program. The script function_09.sh is as follows:

function_09.sh

#!/bin/bash 
# mcd: mkdir + cd; creates a new directory and 
# changes into that new directory 
mcd () 
{ 
   mkdir $1 
   cd $1 
} 
mcd test1 

The preceding script will create the test1 folder in the current folder and change the path to the test1 folder.

A common task in many scripts is to ask users to input an answer as either Yes or No. In such situations, the following script, function_10.sh would be very useful:

function_10.sh

#!/bin/bash 
yesno ( ) 
{ 
   while  true 
   do 
   echo "$*" 
   echo "Please answer by entering yes or no : " 
   read reply 
   case $reply in 
         yes) 
               echo "You answered Yes" 
               return 0 
               ;; 
         no) 
               echo "You answered No" 
               return 1 
               ;; 
         * ) 
               echo "Invalid input" 
               ;; 
               esac 
               done 
         } 
yesno 

Test the script as follows:

$ chmod +x function_10.sh
$ ./function_10.sh

This should produce the following output:

Output:

Please answer by entering yes or no:yes"You answered Yes"$ ./function_10.shPlease answer by entering yes or no:no"You answered No"

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

0 Comments

Submit a Comment

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

one + fifteen =