If any group of commands are repeatedly required, it is not recommended to write these commands separately every time. Repeating the same code increases the length of the script and reduces readability.
Instead, such repeated code can be defined inside a function, which can be called wherever required. A function is essentially a block of reusable commands.
- Reduces the length of the code.
- Improves readability.
- Enhances maintainability.
- Promotes the DRY Principle.
DRY → Don't Repeat Yourself.
There are two ways to define a function in a shell script:
1st Way:
function function_name() {
commands
}2nd Way:
function_name() {
commands
}To call a function, simply use its name followed by parameters (if required):
function_name param1 param2 param3 ...#! /bin/bash
# Defining a function
wish() {
echo "Hello Friends... Good Evening"
}
# Calling the function
wish
wish
wishOutput:
durgasoft@durgasoft:~/scripts$ ./test.sh
Hello Friends... Good Evening
Hello Friends... Good Evening
Hello Friends... Good EveningNote: A function must be defined before it is called.
#! /bin/bash
# Defining functions
f1() {
echo "I am in f1 function"
}
f2() {
echo "I am in f2 function"
f1
}
# Calling functions
f1
f2Output:
durgasoft@durgasoft:~/scripts$ ./test.sh
I am in f1 function
I am in f2 function
I am in f1 functionFunctions in shell scripts can accept parameters. These parameters can be accessed within the function using the following special variables:
$1→ First parameter$2→ Second parameter$@→ All parameters (as a space-separated string)$*→ All parameters (similar to$@)$#→ Total number of parameters$0→ Name of the script (not the function)
#! /bin/bash
function demo() {
echo "First Parameter: $1"
echo "Second Parameter: $2"
echo "Third Parameter: $3"
echo "Total Number of Parameters: $#"
echo "All Parameters with @: $@"
echo "All Parameters with *: $*"
echo "Script Name: $0"
}
# Calling the function with arguments
demo 10 20 30 40 50Output:
First Parameter: 10
Second Parameter: 20
Third Parameter: 30
Total Number of Parameters: 5
All Parameters with @: 10 20 30 40 50
All Parameters with *: 10 20 30 40 50
Script Name: /home/durga/scripts/test.sh#! /bin/bash
wish() {
if [ $# -eq 0 ]; then
echo "Hello Guest Good Evening"
else
echo "Hello $1 Good Evening"
fi
}
# Calling the function with and without arguments
wish
wish Durga
wish RaviOutput:
Hello Guest Good Evening
Hello Durga Good Evening
Hello Ravi Good Evening#! /bin/bash
calc() {
if [ $# -ne 2 ]; then
echo "You should pass exactly 2 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
}
# Calling the function with different arguments
calc 10
calc 20 10
calc 200 100
calc 2000 1000Output:
You should pass exactly 2 arguments
20 + 10 = 30
20 - 10 = 10
20 * 10 = 200
20 / 10 = 2
200 + 100 = 300
200 - 100 = 100
200 * 100 = 20000
200 / 100 = 2
2000 + 1000 = 3000
2000 - 1000 = 1000
2000 * 1000 = 2000000
2000 / 1000 = 2#! /bin/bash
parameter_printing() {
if [ $# -eq 0 ]; then
echo "No parameters passed to this function"
else
echo "All Passed Parameters are:"
echo ".........................."
for p in $@; do
echo $p
done
fi
}
# Calling the function with and without arguments
parameter_printing
parameter_printing A B C D EOutput:
No parameters passed to this function
All Passed Parameters are:
..........................
A
B
C
D
E