Quickstart: How to Write a Function in Bash

In almost all programming languages, we use a function that is nothing more than code written in a logical block to perform repetitive or recursive actions. In shell programming we also have something called as a function in Bash that acts the same. In this post, we will learn about the Bash function.

Basic structure of a Bash function

function_name ( ) {

The structure shown above or the main building block of a Bash function is also referred to as a Bash function qualifier. Make sure you define this Bash function before calling it anywhere.

Bash function example

[[email protected] tmp]# cat function1.sh
# Основная функция
print () {
echo Привет от основной функции AndreyEx
[[email protected] tmp]#

In the above example, we wrote a Bash “print ()” function that prints or echoes when the function is called. As mentioned above, we have a print () function and then call it with the print command below.

Now let’s get down to doing the part.

[[email protected] tmp]# chmod u+x function1.sh
[[email protected] tmp]# ./function1.sh
Привет от основной функции AndreyEx
[[email protected] tmp]

In the above code, we made a shell script function1.sh executable with the command chmod… And, as usual, we executed the script using “./” followed by the name of the shell script.

Passing arguments or parameters

Unlike well-known programming languages ​​like C or Java, we can also pass arguments or script parameters to Bash. Let’s take an example for passing an argument or parameter to a function.

[[email protected] tmp]# cat ./function2.sh
# Передача аргумента или параметра
print () {
echo Привет $1 от AndreyEx
print RedHat_users
print Ubuntu_users
[[email protected] tmp]#

This is the same function we used in the example function, however this time we are passing two parameters or arguments: RedHat_users and Ubuntu_users to the print () function. And we use this argument when repeating the message.

Let’s run the function to see the result.

[[email protected] tmp]# ./function2.sh
Привет RedHat_users от AndreyEx
Привет Ubuntu_users от AndreyEx
[[email protected] tmp]#

Duplicate values ​​with bash function

Unlike the main programming language, we can use a return value that is used to send back the data value to the place of the call.

[[email protected] tmp]# cat function3.sh
# Функция возвращает значение
multiplication () {
expr $1 * $2
ans=$(multiplication $1 $2)
echo Умножение $1 на $2 равно: $ans
[[email protected] tmp]#

In the above example, we are passing two arguments $ 1 and $ 2, which are output during the execution of the shell script. Inside the function, we multiply these arguments and send them back. It is initialized with a variable called “ans” which is used to print the return value.

Let’s take a look at the execution of the same example.

[[email protected] tmp]# ./function3.sh 3 5
Умножение 3 на 5 равно: 15

Reusing actual commands with parameters

We can also write a function with the same command name. Basically we will be wrapping valid commands, providing options for this.

[[email protected] tmp]# cat function4.sh
# Оболочка функции
date () {
command date +%D
[[email protected] tmp]#

In the example of the wrapping command above, we took the “date” command as the function name and we wrapped it with the “+% D” option.

[[email protected] tmp]# ./function4.sh

So ideally our usual command to return the date “Sat Mar 29 19:16:12 BDT 2017” as output, however our wrapping command output gives an output as “03/29/17” due to wrapping.