Functions are reusable code that can be grouped under a single name.

Declare a function Calling a Function Function with arguments Variable scope in Functions

How to declare a Function and call it

Function defination contains the multiple lines of code to executes.

Functions contains a name of a function enclosed in {}.

It Can be declare in two ways


function function_name {
# Commands or valid bash code
# multiple lines
}

function function_name() {
  # Commands or valid bash code
  # multiple lines
}

How to pass a parameters to an function

function_name "parameter1" "parameter2" "parameter3".. "parametern"

Parameters can be access using $1 $2 $3.. $n

function function_name() {
# $1 represents first paramter
# $2 represents second paramter

# $n represents nth paramter
}

Variable scope inside a function