Function And Argument In Shell Script
I am DevOps Engineer who works on DevOps tools like Docker, Kubernetes, Terraform, Git, GitHub, Jenkins and AWS services.
What is Function?
-> If we have to perform repetitive tasks then instead of writing that task every time we write it in a block and whenever needed we run that block and our task gets executed. This block is called a function.
Example - Suppose we have to print "Hello World" 5 times then there is a way to print like -
#!/bin/bash
echo "Hello World"
echo "Hello World"
echo "Hello World"
echo "Hello World"
echo "Hello World"
-> Using this way our task of printing "Hello World" 5 times get completed but suppose we have to print this 1000 times then this will be very time-consuming to do.
-> So instead of writing every time we write our code or task in a block and after that we can call whenever we need.
#!/bin/bash
# There are two ways of definining functions
# 1st Way
function myFunc_1Way {
echo "Hello, Welcome to 1st Way to define Function In Shell Script"
}
# To call the function, Just write the Function Name and Suppose we have to print above line 3 times then instead of writing that code 3 times, we call the function 3 times as -
myFunc_1Way
myFunc_1Way
myFunc_1Way
# 2nd Way
myFunc_2Way() {
echo "Hello, Welcome to 2nd Way to define Function In Shell Script"
}
# To call the function
myFunc_2Way
Output -

Passing Arguments in the Function
-> Suppose we want to take the arguments like taking the values from the user or from the variable then we can do as -
#!/bin/bash
# Variables
name="Bhaskar"
age=23
# Defining function
# We can use this variable values in different ways
echo "1st way of using variable value"
echo "-------------------------------"
myInfo() {
echo "My name is $name"
echo "My age is $age"
}
# Calling the Function
myInfo
echo
echo "2nd way of using variable values"
echo "--------------------------------"
myInfo() {
echo "My name is $1"
echo "My age is $2"
}
# Calling the Function
myInfo $name $age
Output -

-> We can take values fro the users also as -
#!/bin/bash
# Taking values from user
read -p "Enter your name : " name
read -p "Enter your age : " age
# Defining function
# We can use this variable values in different ways
echo "1st way of using variable value"
echo "-------------------------------"
myInfo() {
echo "My name is $name"
echo "My age is $age"
}
# Calling the Function
myInfo
echo
echo "2nd way of using variable values"
echo "--------------------------------"
myInfo() {
echo "My name is $1"
echo "My age is $2"
}
# Calling the Function
myInfo $name $age
Output -

Passing Argument While Running Shell Script
-> We can pass the argument when we run the shell script
Ex - bash runtime_args_func.sh <first argument> <second argument> ...
#!/bin/bash
echo "First argument is - $1"
echo "Second argument is - $2"
-> Suppose we run this as - bash runtime_args_func.sh Bhaskar Mehta
-> Here the first argument is Bhaskar and the second argument is Mehta
Output -

-> We can also get how many total arguments have been passed and a list of all the arguments as-
-> To get the total number of arguments use - $# and To get the list of all arguments use - $@
#!/bin/bash
echo "First argument is - $1"
echo "Second argument is - $2"
# To get the total arguments
echo
echo "Total arguments are - $#"
# To get list of all arguments
echo
echo "List of argumnets are - $@"
Output -

-> We can apply for loop on the arguments -
#!/bin/bash
echo "First argument is - $1"
echo "Second argument is - $2"
# To get the total arguments
echo
echo "Total arguments are - $#"
# To get list of all arguments
echo
echo "List of argumnets are - $@"
# Fro loop on the tatal list of arguments -
for args in $@
do
echo "Argument is $args"
done
Output -

-> When we pass multiple arguments then based on the requirement we can shift the arguments.
-> Suppose we have passed three arguments A B and C and suppose our task is done with argument A and now we want to combine the argument B and C then we can Shift the Argument.
Ex -
#!/bin/bash
echo "First argument is - $1"
# If we want to combine the remaing Argument then we can use shift
shift # Here we are saying that 1st argument is gone, now treat remaing arguments as a single arguments
echo "Second argument is - $@"
Output -

