Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024
Difficulty: Easy

Linux - Shell Variables

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


A variable is a place in memory where a value is kept. This place has a name to make it more descriptive. Any sort of data, such a name, number, text, or filename or directory, could be the value. A variable is therefore nothing more than a reference to a specific piece of data. A user can create, assign, or delete variables using a shell. These variables, however, are just momentary and are immediately erased when the shell session ends. A shell variable must be exported in order to become an environment variable and become persistent and system-wide. The command to use depends on the particular shell being used. The command is "export" in Linux's Bash shell.

Introduction to linux shell variables.


The syntax "variable_name=variable_value" is used to define a shell variable. For instance, the shell variable "My_Name" is created with the value "Ninja" when the command "set My_Name=Ninja" is used. Values with spaces require the use of quote marks. Although it is not necessary. Additionally, variable names and filenames under Unix are case-sensitive.

Variable Names

Only letters (a to z or A to Z), numbers (0 to 9) and the underscore character (_) are permitted in a variable's name.

Unix shell variables will often have their names in UPPERCASE.

These are legitimate instances of variable names. 


These are instances of invalid variable names. 


The reason you cannot use other characters such as !, *, or - is that these characters have a special meaning for the shell.

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

Managing the environment variables

A character string given a value is referred to as a variable. The value assigned could be any kind of data, including a number, text, filename, device, or other data. A variable is simply a reference to the data itself. You can create, assign, and delete variables using the shell.

Creating Variables 

Variables can be made at the shell level or using shell scripts. A shell script loses all variables it has created when it stops running. However, a variable defined at the prompt will be present until the shell is ended. The syntax for defining a variable is as follows: 

<variable_name> = <variable_value>

Note: There should be no space between the ‘=’ operator while assigning the values. 

For Example: assign the value “CODING_NINJAS” to variable “MY_VAR”, and then use the echo command to verify the value to a variable we have assigned. 

Creating Variables examples.

Case sensitive 

Variables in Linux are case sensitive, to show that they are case sensitive.

Let’s look at an example to see that they are case-sensitive.  

Case sensitive examples


Double quotes in the shell still allow for parsing of the variables while single quotes don’t allow this. 

Quotes Examples.

Dollar Sign($) 

The dollar sign $ is another significant character that the shell can translate. The shell will search for an environment variable with a name similar to the string after the dollar sign and substitute the variable's value there (or with nothing if the variable does not exist).

Examples employing $HOSTNAME, $USER, $UID, $SHELL, and $HOME include these.

Dollar Sign Examples.

Read-only variables 

Shell offers the read-only command as a means of designating variables as read-only. A variable's value cannot be modified once it has been designated as read-only.

For instance, while attempting to modify the value of Name, the script below generates an error.

 Read only variables.

Unsetting variables 

When a variable is unset or deleted, the shell is instructed to take it off of the list of variables it keeps track of. A variable's stored value cannot be accessed after it has been unset.

Here is a simple example that demonstrates how the command works −

Unsetting Variables.

The above example does not print anything. You cannot use the unset command to unset variables that are marked read only.


The shell searches for commands to execute in various locations using the $PATH variable (unless the command is builtin or aliased). The entire pathnames of the directories are listed in this variable and are separated by colons. 

Path Variables.


A list of exported variables will be displayed by the env command without any parameters. Set lists all variables, even those not exported to child shells, in contrast to set with options. However, env can also be used to launch a fresh shell (a shell without any inherited environment). The environment for the subshell is cleared by the env -i command. 

Env Variables

The $LANG or any other variable can be set for only one instance of bash using the env command. This is used in the example below to demonstrate how the $LANG variable affects file globbing. 


With the export command, you can transfer shell variables to different shells. With this, the variable will be exported to child shells.

Export examples

Nevertheless, it won't export to the parent shell (the previous screenshot continued). 


Single embedding is a practical way to keep your current directory intact. To embed, the screenshot below utilizes backticks rather than dollar signs.


Backticks cannot nest embedded shells; only the $() notation can do so.

Backticks or single quotes 

The embedding takes up one fewer character than the dollar and parenthesis combination when placed between backticks. But be careful—single quotes and backticks are frequently misinterpreted. There is a big technical difference between and!

Backtick examples

Frequently Asked Questions 

Write a command to list all the environment shell variables.

Env is the command to list all the environment shell variables. 

What are the types of variables in Linux?

There are three types of variables i.e. local, environment, and shell variables. 

How to destroy a declared variable? 

We can use the command unset<variable name>. 

What is the use of the echo command in Linux? 

A shell variable is a unique variable that is set by the shell and is necessary for the shell to operate properly. While others are local factors, some of these variables are environment-related. 

What is the scope of the environment variable? 

The scope of environment variables can be local or global. 


We discussed the shell variables in Linux and managing the environment variables in the shell and then discussed some of the frequently asked questions from shell variables in Linux. 

For Related Blogs, please refer to:

  1. Types of Unix Operating Systems
  2. Introduction to Linux Shell and Shell Scripting
  3. Ubuntu Operating System
  4. Touch command in linux
  5. wget command in linux

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available; look at the Top 150 Interview Puzzles interview experiences, and interview bundle for placement preparations. Read our blogs on aptitudecompetitive programminginterview questionsIT certifications, and data structures and algorithms for the best preparation. 

Topics covered
Variable Names
Managing the environment variables
Creating Variables 
Case sensitive 
Dollar Sign($) 
Read-only variables 
Unsetting variables 
Backticks or single quotes 
Frequently Asked Questions 
Write a command to list all the environment shell variables.
What are the types of variables in Linux?
How to destroy a declared variable? 
What is the use of the echo command in Linux? 
What is the scope of the environment variable?