Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Shell Scripting - Functions
2.1.
Declaring Functions in Shell Scripting
2.2.
Calling Functions in Shell Scripting
2.3.
Passing Arguments in Shell Scripting
3.
Shell Scripting - Variables
3.1.
Global or Shell Variables
3.2.
Local Variables
3.3.
Environment Variables
4.
Frequently Asked Questions
4.1.
What are the three types of variables available in Linux?
4.2.
Which command is used to print all the Environment variables?
4.3.
In which year was Linux introduced, and by whom?
4.4.
What does the ‘cat<< EOF’ operator do?
4.5.
Define String Operator.
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Shell Scripting - Functions & Variables

Author Rupal Saluja
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

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

logo of linux

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.

logo of shell scripting

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. 

  1. By using the ‘function’ keyword
  2. By avoiding the ‘function’ keyword.

Use the image below to get an appropriate syntax for both cases.

two ways to declare functions

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.

first way of function declaration

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.

second way of function declaration

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

rummin the script

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.

calling functions

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.

importance of 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.

importance of 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.

importance of sequence

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,.........

arguments example

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.

passing arguments

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Shell Scripting - Variables

There are three types of variables available in Shell Scripting. These are Local Variables, Environment Variables, and Shell or Global Variables. Each of these has its importance and is used as per the user’s requirements.

Global or Shell Variables

By default, every variable which you declare in your script is a global variable. That means we can access it from anywhere in your script. 

We will use the same ‘print’ function previously used for our example. To illustrate, we will create a variable ‘name’ and assign the first argument ‘$1’ to it. This means any value passed using the first argument becomes the value of variable ‘name’. Using the echo function, we will print the value of this variable. Use the image below for a better understanding.

declaring global variables

Local Variables

Let us understand local variables using an example and how to use them.

Sometimes, we want the value we gave inside a function to not be changed outside the function. For that, we will use the ‘local’ command. Your variable becomes strictly local whenever you add a ‘local’ command. That means you won’t be able to access the variable outside the function. Use the image below for a better understanding.

declaring local variables

In the image above, we see that ‘Tom’ is first assigned to the variable ‘name’. Then, the print function is called, which assigns ‘Max’ to the variable ‘name’. Again, if we print the value of variable ‘name’, it is changed to ‘Tom’. This is because ‘Max’ is local to the print function.

Environment Variables

Environment Variables, acronymed as ENVs, determine the behavior of the environment. These variables can affect the ongoing processes or other programs executed in that environment.

Various use cases include Execution Mode, Domain Names, API URLs or URIs, Service Account Names, etc.

Frequently Asked Questions

What are the three types of variables available in Linux?

Linux's three types of variables are local variables, environment variables, and shell variables.

Which command is used to print all the Environment variables?

The ‘env’ command is used to get all the Environment variables.

In which year was Linux introduced, and by whom?

Linus Torvalds introduced Linux in the year 1991.

What does the ‘cat<< EOF’ operator do?

The ‘cat<< EOF’ operator denotes the end of the file. It informs the compiler or interpreter that the file it was reading has been completed as soon as this operator arrives.

Define String Operator.

The operators specially used for files are known as String Operators. These operators are generally used to check the different properties of strings.

Conclusion

To cut it short, we understood what Functions and Variables are in Shell Scripting. We learned how to declare functions, call functions, and pass arguments in functions. We also discussed the three types of variables and their importance.

We hope the above discussion helped you understand Shell Scripting- Functions and Variables clearer and can be used for future reference whenever needed. To learn more about Ready API and its components, you can refer to blogs on Shell Scripting Interview QuestionsShell LoopsShell Scripting- Arithmetic Operations, and Shell Variables.

Visit our website to read more such blogs. Make sure you enroll in our courses, take mock tests, solve problems, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.

Happy Coding!

Live masterclass