Function in Linux Shell Scripting – new

In a function, we wrap up a block of code that performs a particular task and that can be called from the later part of the code. In shell scripting, a function must be defined at the upper portion of the code before it is called.

Look at the below code where a function “addNumbers” is used to add two numbers and print the summation: save the following code as function_sum.sh

#!/usr/bin/env bash

#Functions to add two numbers
function addNumbers() {
  local number1=10
  local number2=50
  local sum=$((number1+number2))
  echo "Summation is: $sum"
 }
 
 #Call the function
 addNumbers

Output:

$ bash function_sum.sh
 Summation is: 60

Function’s name generally starts with a “verb” (e.g.: sayHello, addNumbers, calculateBalance, etc) as we do/solve a task in a function.

We can write function name both in camelCase (e.g.: generateReport, start with lower case for the first word and then start with the upper case for the other words) or we can user underscore (e.g.: generate_report) to separate the words in a function name. Again, we can define a function with and without the “function” keyword.

Let’s see the following example: save the following code as function.sh

#!/usr/bin/env bash

#Function with function keyword
#Functions name starts with a verb
function sayHello() {
  local NAME=$1
  echo "Hello $NAME. Welcome to shellscripting."
 }

#Function without function keyword
sayGoodbye() {
  echo "Goodbye $1. See you later."
}

#Functions are invoked as normal shell command
echo "Calling the sayHello function with param"
sayHello Olive

echo "Calling the sayGoodbye function"
sayGoodbye Olivia

exit 0

Output:

$ bash function.sh
Calling the sayHello function with param
Hello Olive. Welcome to shellscripting.
Calling the sayGoodbye function
Goodbye Olivia. See you later.

Shell Script Function with Parameter(s):

We can pass parameters when we call a function.

$1, $2, $3,…, $N can be used to get all function parameters.

$0 often refers to the name of the shell script.

Let’s modify the “addNumbers” function and pass two numbers as parameters: save the following code as function_sum_with_param.sh

#!/usr/bin/env bash

#Functions to add two numbers
function addNumbers() {
  #Getting the parameters' values and assign to number1 and number2
  local number1=$1
  local number2=$2
  local sum=$((number1+number2))
  echo "Summation is: $sum"
 }
 
 #Call the function and passing the parameter
 addNumbers 10 50

Output:

$ bash function_sum_with_param.sh
Summation is: 60
Return value from a Shell Script Function:

We can also return a value from a function. Let’s modify again the “addNumbers” function to return a value: save the following code as function_sum_return_val.sh

#!/usr/bin/env bash

#Functions to add two numbers
function addNumbers() {
  local number1=$1
  local number2=$2
  local sum=$((number1+number2))
  #Returning the value, 
  #echo will act as a return statement if we call this function like below with $
  echo "$sum"
 }
 
 #Call the function and passing the parameter, assiging the returned value to 
 #new variable summation
 summation=$(addNumbers 10 50)
 echo "Summation is: $summation"

Output:

$ bash function_sum_return_val.sh
Summation is: 60

Follow us on social media
Follow Author