Learn to Code via Tutorials on Repl.it!

← Back to all posts
Shell Scripting S22 Shell Script Functions- How to define,call and pass parameters
sasivanga (15)

Session-22. Shell Script Functions- How to define,call and pass parameters

If any group of commands repeatedly requited ,then it is not recommended write these commands separately every time because it increases length of code and reduces readability
Such type of repeatedly required commands we have to define inside a block and we can call that block every time where ever required .The block of commands called function
Function is a group of repeatedly required commands

Advantages of function :

1)It reduces length of code
2)It improves readability
3)It improves maintainability
4)It promotes DRY principle DRY⇒ Dont Repeat Yourself

Functions Syntax:

1st way :
Function function_name()
{
Repeated commands
}

2nd way :
function name() ⇒ It always no arguments even we passing arguments
{
Repeated commands
}

How to call a function :
function_name param1 param2 param3
function_name ⇒ no parameters
m1();
f1();

Q)Write a function to display wish message

#! /bin/bash
function f1()
{
    echo "hello"
}
f1
f1
f1

We can define function anywhere
Rule: We can define a function anywhere ,but before calling the function it should be defined

Functions with Parameters :

Functions can accept parameters also ,which are input to the function
Within the function we can access parameters as follows :
$1 ⇒ First parameters
$2 ⇒ . Second parameter
[email protected] ⇒ All parameters
$*⇒ All parameters as single string and separated with IFS -Default space
$# ⇒ the number of parameters
$0 ⇒ Not function name and it is script name

#! /bin/bash
function f1()
{
    echo "enter first parameter $1"
    echo "enter first parameter $2"
    echo "Total number of  parameter $#"
    echo "All parametrts @ [email protected]"
    echo "All parametrts * $*"
    echo "Script name : $0"
    }
f1 10 20 30 40 50

Output : 

enter first parameter 10                                    
enter first parameter 20                                    
Total number of  parameter 5                                
All parametrts @ 10 20 30 40 50                             
All parametrts * 10 20 30 40 50                             
Script name : main.bash

Good morning based on arguments

Q) Write function to display

#! /bin/bash
function wish()
{
    if [ $# -eq 0 ]; then
        echo "hello guest  how are you "
    else
        echo "$1 good morning "
    fi
}
wish sasi

Q)write a program for calculator using functions

#! /bin/bash
calc()
{
    if [ $# -ne 2 ]; then
        echo "Pass exactly two arguments "
    else
        a=$1
        b=$2
        echo "$a + $b = $[a+b]"
        echo "$a - $b = $[a-b]"
        echo "$a * $b = $[a*b]"
        echo "$a / $b = $[a/b]"
        echo
        
    fi
}
calc 10 200
calc 10 20
calc 20 10

Q)Write a function to print all parameters ?

#! /bin/bash
paramp()
{
    if [ $# -eq 0 ]; then #no parameters passed thi sfunction 
            echo "no parameters passed this function "
    else
       echo "All parameters passed is"
        echo "-----------------------"
        for param in [email protected]
        do
        echo $param
        done
        fi
}
paramp 10 20 30 40