Introduction
If you have ever used Linux, you must have used Shell Scripting. Shell Scripting is the phenomenon of creating shell scripts.

A shell script or shell program is a simple computer program containing a set of shell commands. This program is compiled by the UNIX shell, a command language interpreter. This means the shell processes the command a single line at a time. It differs from C or C++ programs translated entirely into a binary image through a compiler.

This article will discuss one of the most important foundational concepts of Shell Scripting, its functions, and variables. So, without further ado, let’s start.
Shell Scripting - Functions
Before starting with Shell Scripting, ensure you have a Linux Operating System. Examples of Linux Operating Systems are Ubuntu, Devian, Fedora, Deepin, etc. Also, you are good to go if you have macOS or Windows 10. This is because nowadays, MacOS and Windows 10 also support shells.
Declaring Functions in Shell Scripting
Like any other programming language, shell programs also support functions. However, they support a few limited implementations. So, firstly what is a function?
A Function is a subroutine or a code block that executes a set of operations. It is a part of code with some name that performs some functionality. Once declared, a user can use this function once or multiple times. There are two ways to declare functions.
- By using the ‘function’ keyword
- By avoiding the ‘function’ keyword.
Use the image below to get an appropriate syntax for both cases.

Examples
We will use two example shell programs for a better understanding.
The first example will print ‘Hello’ using a function. For this example, we will be using the ‘function’ keyword. We will create a function with the name ‘Hello’. Use the image below for the accurate syntax.

The second example is called to exit the shell program. For this example, we will not use the ‘function’ keyword. We will create a function with the name ‘quit’. Use the example below for accurate syntax.

We will run the script to check if anything is printed as output. Nothing is printed because we have not called the function yet.

Calling Functions in Shell Scripting
We have seen two example declarations. Now, we will call these functions. To call a function, you just have to mention the function name. For Example, if you want to run the first example function, you must add its name, ‘Hello’, in the script. Use the image below for a better understanding.

Remember that the functions are executed in the same sequence in which they are called. For Example, if we call the second example function before the first one, the program exits. This is because the program will not even read the first example function and the echo command, which comes later in the sequence.

We will now change the sequence to see if anything changes. We will call the first example function before the second one. The first function executes, but the echo command doesn’t because it comes later in the sequence.

We will again change the sequence. We will place the echo command before the second example function. Both the first function and the echo command execute.

Passing Arguments in Shell Scripting
To understand this better, we will use an example function, ‘print’.
An argument is a combination of a dollar symbol ‘$’ and a positive integer that denotes the index of an argument. For reference, an argument is written as $1, $2, $3,.........

Now, we will call the function ‘print’, passing an argument. We will add any argument after the function's name to pass an argument. Use the image below for a better understanding.

The word ‘Hello’ is assigned to the $1 as the first argument.