Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»How to Use Functions in Bash Shell Scripts

    How to Use Functions in Bash Shell Scripts

    RahulBy RahulJuly 28, 20132 Mins Read

    What is function:

    A function which can also be referred to as subroutine, procedure is a block of code used for specific tasks. Function’s also have a property called reusability.

    This tutorial will help you to how to create and use functions in shell scripts.

    Create First Function in Shell Script

    Create your first function in shell script showing output “Hello World!”. Create a shell script “script.sh” using following code.

    # vim script.sh
    
    #!/bin/bash
    
    funHello(){
        echo "Hello World!";
    }
    
    # Call funHello from any where in script like below
    
    funHello
    
    

    Execute Script

    # sh script.sh
    ouput:
    
    Hello World!
    

    How to Pass Arguments to Function in Shell Scripts

    Passing argument to functions is something same like to pass argument to command from shell. Functions recieves arguments to $1,$2… etc. Create a shell script using following code.

    # vim script.sh
    
    #!/bin/bash
    
    funArguments(){
       echo "First Argument : $1"
       echo "Second Argument : $2"
       echo "Third Argument : $3"
       echo "Fourth Argument : $4"
    }
    
    # Call funArguments from any where in script using parameters like below
    
    funArguments First 2 3.5 Last
    
    

    Execute Script

    # sh script.sh
    Ouput:
    
    First Argument : First
    Second Argument : 2
    Third Argument : 3.5
    Fourth Argument : Last
    
    

    How to Receive Return Values from Functions in Shell Scripts

    Some times we also need to return values from functions. Use below example to get returned values from functions in shell scripts.

    # vim script.sh
    
    #!/bin/bash
    
    funReturnValues(){
    echo "5"
    }
    
    # Call funReturnValues from any where in script and get return values
    
    values=$(funReturnValues)
    echo "Return value is : $values"
    
    

    Execute Script

    # sh script.sh
    Ouput:
    
    5
    

    How to Create Recursive Functions in Shell Script

    Functions which calls itself are called recursive functions. Following example is showing to print 1 to 5 digits with recursive function.

    # vim script.sh
    
    #!/bin/bash
    
    funRecursive(){
    val=$1
    if [ $val -gt 5 ]
    then
    	exit 0
    else
    	echo $val
    fi
    val=$((val+1))
    funRecursive $val     # Function calling itself here
    }
    
    # Call funRecursive from any where in script
    
    funRecursive 1
    
    

    Execute Script

    # sh script.sh
    Ouput:
    
    1
    2
    3
    4
    5
    
    bash functions scripts shell scripts
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleMySQL Dump/Restore Stored Procedures and Triggers
    Next Article How to Delete Files Older than 30 days in Linux

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Bash Select Command (Create Menu in Shell Scripts)

    Updated:December 10, 20212 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    Bash String Concatenate

    Updated:January 5, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.