Functions in Bash Scripting | Bash Functions

functions-in-bash-scripting
  • Save

In this blog, we will discuss the Function of Bash Scripting, Don’t worry we always think about the precious time of the reader. In this blog, we will discuss What is a function in the bash script. What are the different types of functions in a bash script? Why do we use the function in bash and many things related to that? So let’s get started with the blog.

Table of Contents

Introduction to Function in Bash Scripting

Functions in Bash scripting can be defined as a set of commands which can be reused several times with the bash script.

Functions in bash are there to make your scripts easier to read and prevent you from writing the same code repeatedly.

It helps the programmer to divide long, complex code into small pieces of code that can be reused anywhere whenever the programmer needs he/she can use it.

We can reuse, optimize, and simplify the code by calling functions frequently and at any time.

Advantages of Function in Bash Scripting

  1.  It provides modularity in the program and reduces the code length.
  2. We can use that file for executing the set of commands one or more times as per our requirements. 
  3. The function in the bash makes the program more readable to the programmer.
  4. These instructions can be typed directly into the command line, but from the point of reuse, it is useful to collect all the relevant instructions for a certain task in a single file, which makes that file reusable in the program.

Key points for writing Functions in Bash Scripting

  1. Before we can call the function, it must be declared in the shell script.
  2. Arguments can be passed to the functions and accessed inside the function as $1, $2, etc.
  3. Built-in commands of the Bash shell can be overridden using functions.
  4. Within the function, local variables may be assigned, and their scope will be limited to that specific function.

The Syntax for writing Functions in Bash Scripting

The syntax for declaring a bash function can be defined in two formats:

A) Start with the function name, followed by parentheses.

function_name () {  
commands  
} 

b) Starts with the function reserved word, followed by the function name

function function_name {  
commands  
} 

Types of Functions in Bash Scripting

1) Simple Function Declaration in Bash Scripting

#!/bin/bash

## Printing Hello Bash Example

## First Type Syntax
BashFirst(){
        echo "Hello Bash Programming First Method Type"
}

BashFirst


## Output:
Hello Bash Programming First Method Type

#!/bin/bash

## Printing Hello Bash Example

## Second Type Syntax

function BashSecond(){
        echo "Hello Bash Programming Second Method Type"
}

BashSecond


## Output:
Hello Bash Programming First Method Type

2) Passing Arguments in Function

#!/bin/bash

## Function Arguments

print_arguments(){
        echo $1
        echo $2
        echo $3
}

print_arguments "Bin""Fin""Tech"

#!/bin/bash

## Function Arguments
## Addition of two numbers

sum(){

        echo "Addition of two numbers is:" $(($1+$2))
}

sum 8 9

3) Variable scope in Function in Bash Scripting

#!/bin/bash

## Scope in function

emp1="Alex"
emp2="Sam"

function getEmployeeName(){
        local emp1="Copper"
        emp2="Travis"

        echo "We are inside the function"
        echo $emp1
        echo $emp2
}


echo "Executing before the function call"
echo $emp1
echo $emp2


getEmployeeName

4) Return Value of Function in Bash Scripting

#!/bin/bash

## Return Function Demo

function getName(){
        company_name="BinFin Tech"
        echo $company_name
        echo $1
        return 5
}

getName "Company Name"

echo "The previous function returned value is $?"

#!/bin/bash

## Alternative return function

function getName(){
        local company_name="BinFin Tech"
        echo $company_name
}

company="$(getName)"
echo $company

5) Overriding Commands of Function in Bash Scripting

#!/bin/bash

## Override Functions

function echo(){
        builtin echo "Company name is: $1"

}

echo "BinFin Tech"

FAQ

What is a function in bash?

Function in Bash scripting can be defined as a set of commands which can be reused several times with the bash script.
Functions in bash are there to make your scripts easier to read and prevent you from writing the same code repeatedly.

How to write a function in bash?

We can write a function in two different formats
1) Start with the function name, followed by parentheses.
function_name () {
commands
}

2) Starts with the function reserved word, followed by the function name
function function_name {
commands
}

How to call a function in a shell script or bash script?

Just write the name of the function.

What are $@ and $* in a shell script?

$* – It stores a complete set of the positional parameter in a single string.
$@ – Quoted string treated as a separate argument.

Write blogs related to Ethical hacking, Computer networks, Linux, Penetration testing and Web3 Security.

Leave a Reply

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

Back To Top
0 Shares
Share via
Copy link