Magazine

Passing Arguments Or Parameters to Functions Using Linux Bash Script

Posted on the 24 April 2021 by Satish Kumar @satish_kumar86

In certain situations, wemayneed to pass arguments or parameters to functions. In such situations, we canpassarguments 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, calledfunction_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 thetest1folder in the current folder and change the path to thetest1folder.

A common task in many scripts is to ask users to input an answer as eitherYesorNo. In such situations, the following script,function_10.shwould 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"

Back to Featured Articles on Logo Paperblog