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.

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.

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

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.

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.

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 −

The above example does not print anything. You cannot use the unset command to unset variables that are marked read only.
Path
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.

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

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.
Export
With the export command, you can transfer shell variables to different shells. With this, the variable will be exported to child shells.

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

Backticks
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!

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.
Conclusion
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:
- Types of Unix Operating Systems
- Introduction to Linux Shell and Shell Scripting
- Ubuntu Operating System
- Touch command in linux
-
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 aptitude, competitive programming, interview questions, IT certifications, and data structures and algorithms for the best preparation.