Table of contents
1.
Introduction
2.
Comments
2.1.
Single-line comments
2.2.
Multi-line comments
2.2.1.
Using << ‘some_word’ … ‘some_word’
2.2.2.
Using : ‘…’
3.
Variables
3.1.
User-defined variables
3.1.1.
Naming Variables
3.1.2.
Defining Variables
3.2.
Environment Variables (Global Variables)
3.3.
System Variables or Shell Variables
3.4.
Using Variables 
4.
Frequently Asked Questions
4.1.
How can we add multiple line comments in Shell scripts? 
4.2.
What is the correct way to declare variables in Shell scripts? 
4.3.
What is the difference between environment variables and shell variables? 
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Comments And Variables in Shell Scripting

Author Neha Chauhan
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A shell is a command-line interpreter that allows the users to write programs to interact with the operating system. A shell script can help you automate the command line actions. You can create a shell script of the actions that you perform every day repetitively in a file and then run that file, instead of performing those actions manually. 

Whenever we write code, we try to add comments to the code, these comments can be details about the code or some other information. In shell scripting, we can add comments using a ‘#’. 

Variables are containers to store data. 

Comments and Variables in shell scripting

In this article, we will discuss how to add comments to shell scripts and how to create and use variables in shell scripts. 

Comments

Adding comments that provide information/details about the code is a good practice while writing a code. Commented lines do not get executed. You can add both single-line and multi-line comments. 

Single-line comments

A single-line comment starts with a ‘#’ (hash-tag). A line starting with ‘#’ will not get executed. 

Example-

#This is code 
echo “First program”

Multi-line comments

There are two ways of writing multi-line comments. 

Using << ‘some_word’ … ‘some_word’

Try to understand this by taking an example-

<< comment 
This is line 1. 
This is line 2. 
This is line 3. 
comment 

Or

<< com 
This is line 1. 
This is line 2. 
This is line 3.
com 

🐧 The comment block should start with ‘<<’, followed by any word like - comment, com, example, info, etc. 

🐧 End the block of comments with the same word that was used at the start of the comment. 

Everything in between this block will be treated as a comment and will NOT be executed. 

Using : ‘…’

Try to understand this by taking an example -

: ‘
This is line 1. 
This is line 2. 
This is line 3.
’

🐧 A comment block will start with : ‘

🐧 The block will end with a 

Variables

Variables are the containers used to store data. In shell scripts, a variable can store any type of data, like - string, integer, real number, etc. 

User-defined variables

The variables defined by the user are called user-defined variables. These variables are local variables. 

You can create global user-defined variables also, we have covered this later in the article.

Naming Variables

These variables can be defined in lower-case as well as upper-case. 

The variable name can be written using alphabets (a-z), numbers and underscore ‘_’

Note: The variable name cannot start with a number. 

Example -

My_var

MY_VAR

var

newVariable 

Variable1

Var_1

Defining Variables

To define a variable, write the variable name on the left side of the ‘=’ (equal to) sign and on the right side write the value of the variable.

Example - 

my_var=ninja

 

Note: Do not give any space between the variable name, ‘=’ and value of the variable i.e my_var=ninja is valid but my_var = ninja is NOT valid and it will give an error.

 

These variables can hold only one value at a time, so in order to enter multiple-word sentences as the value of a variable, we will have to enclose it in “..” (double inverted commas) i.e. “Coding Ninjas” 

Example -

MyVariable = “Coding Ninjas” 

Environment Variables (Global Variables)

The Environment variables are accessible to all the shells. 

They can be pre-defined by the operating system or can be user-defined. 

To set a global variable, use the “export” command. 

Example -

$ export my_var=ninja (my_var defined in the environment) 
$ my_var2=ninja2 (my_var2 defined in shell) 
$ bash (Start new shell)
$ echo $my_var 

Output of above line - ninja (echoes value of my_var) 

$ echo $my_var2 (empty)

Output of the above line will print nothing as my_var2 is a local variable and is not accessible when we start a new terminal. 

In the above example, my_var is a global variable so when we change the shell, it is still accessible however, my_var2 is a local variable that is not accessible outside the shell in which it was declared. 

NOTE: The environment variables are available for the shells that have been started after the variable was declared.

System Variables or Shell Variables

The system variables are defined by the operating system. Their values cannot be changed. These variables are required for the shell to operate properly.

 We cannot change the name of the system variables. These variables are usually written in capital letters. 

Some system variables are - 

$BASH - It will output the name of the shell you are using. 

$HOME - It will give the name of the home directory. 

$PWD - It will give the address of the present working directory. 

$HOSTNAME - It will give the name of the host. 

Using Variables 

To use a variable add ‘$’ (dollar sign) before the name of the variable. 

Example - 

my_name = “Ninja Coder” 
echo $my_name

Frequently Asked Questions

How can we add multiple line comments in Shell scripts? 

There are two methods to write multiple-line comments, you can start the comment with << followed by any word and finish the comment block with the same word that you used to start the comment block. Another method is to start with :’ and end the comment block with ‘. 

What is the correct way to declare variables in Shell scripts? 

The name of the variable goes to the left of the ‘=’ (equal to sign) and the value of the variable goes to the right. There should not be any space between the name of the variable, ‘=’ and the value of the variable. For Example, my_variable=ninja. 

What is the difference between environment variables and shell variables? 

Environment variables are global variables that can be accessed by all the shells that were formed AFTER the variable was declared. 

Shell variables are system-defined variables with pre-defined names and values. They are local to the shell in which they are declared, i.e. they cannot be used outside of the shell in which they were declared.  

Conclusion

Congratulations on finishing this article. In this article, we discussed how to add comments to the shell script and how to create, declare and use variables in shell scripts. We also discussed about different types of variables in shell scripts. 

Do not stop learning. We recommend you read some of our articles - 

🔥 Introduction to Linux

🔥 Linux Kernel

🔥 Linux Directories

🔥 Linux Commands list

Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more courses.

If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to the Coding Ninjas Studio, our practice platform.

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass