Linux Environment Variables

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

In this article, we will learn different things about the Linux environment. When I say Linux environment, I mean the environment variables present on the Linux-based operating systems.

We will go through what a variable is, what are the different types of variables, How to list or view the environment variables, how to create a new Linux environment variable, how to set a local Linux environment variable and so many other interesting topics. So buckle up, pay attention and read right to the end.

What is an environment variable?

Before we even address what an environment variable is, let us discuss what a regular variable is. In layman’s language, a variable is an unknown quantity. Since we are talking Linux, let us see it from a technical angle.

In the technical world, a variable is a location for storing any value that can be changed depending on conditions passed to the application or program. If variables did not exist, writing programs would be a million times harder than it is today. Variables help us write flexible programs.

Now that we have seen what is a variable, let us look at what an environment variable is. Environment variables are dynamic values that affect the programs that are running on a server.

Environment variables exist on every single operating system on earth and they can be created, deleted, saved, or edited.

The environment variables are placeholders (since they are empty that can take any value) for information stored within the system that passes data to programs launched in the shells.

Environmental variables define the environment’s behavior. They allow you to customize how the system works and the behavior of the applications on the systems. For example, the environment variables can store information about a browser, text editor, path to executable files, keyboard settings and so many more.

Some environmental variables are already set by the system and others can be set by you. A few of the variables are also set by shells or any other program that loads into the system.

Scope of environment variables

Scope here means, from what region can the variable be accessed. An environment variable can have a scope of local or global.

1. Global Environment Variable

An environment variable with global scope can be accessed from anywhere in that specific environment that exists in the terminal. The variable can be used in all kinds of programs, processes, or programs running in the environment within the terminal.

2. Local Environment Variable

An environment variable with a local scope cannot be accessed by any program, application, or process running in the terminal. The variable can only be accessed by the terminal itself.

Environment variables vs shell variables

Variables in an operating system can be classified into 2 main types, shell variables, and environmental variables. Even though we are discussing ENVs, let us give a brief moment to understand shell variables also.

Shell variables

These are the variables that apply only to a certain shell. Each shell, like bash, Korn, bourne, zsh, etc, have their own set of internal shell variables.

Environment variables

These are the variables that are inherited by all shield processes and shells and are available system-wide.

Rules for writing variables in the terminal

We cannot create variables as per our own rules, we need to follow the below-listed formats while creating variables in the terminal.

1. The names of the variables are case-sensitive. By convention, all the variables created must have upper case names.

2. When you need to assign multiple values to a single variable, you must separate the values by a colon (:).

3. You must give no space before and after the equals (=) sign.

For example: VAR=<value_1>:<value_2>

After creating a variable, you must also follow a rule while writing it in the terminal. No matter which variable’s name you are writing in the terminal, (either created or default) you will have to specify a dollar ($) sign in front of it. However, while redefining variables, do not use the dollar ($) sign

Environmental variables in Linux

Before we even operate on the variables (like creating, listing, etc), let us take a look at some of the most important variables in the Linux-based operating systems.

1. DISPLAY

This variable contains the identifier for the display that X11 programs should use by default.

2. HOME

This variable indicates the home directory of the current user: the default argument for the cd built-in command.

3. IFS

This variable indicates the Internal Field Separator that is used by the parser for word splitting after expansion.

4. LANG

This variable expands to the default system locale; LC_ALL can be used to override this.

5. LD_LIBRARY_PATH

This variable contains a colon-separated list of directories that the dynamic linker should search for shared objects while building a process image after exec, before searching in any other directories.

6. PATH

This variable indicates the search path for commands. It consists of a colon-separated list of directories in which the shell looks for commands.

7. PWD

This variable indicates the current working directory as set by the cd command.

8. RANDOM

This variable generates a random integer between 0 and 32,767

9. SHLVL

This variable increments by one each time an instance of bash is started.

10. TERM

This variable refers to the display type.

11. TZ

This variable refers to the Time zone. It can take values like GMT, AST, and many more.

12. UID

This variable Expands to the numeric user ID of the current user, initialized at the shell startup.

13. USER

This variable contains the username.

14. SHELL

This variable contains the shell that is being used by the user.

15. EDITOR

This variable contains the path to the program which edits the content of files.

16. OLD_PWD

This variable contains the previous working directory.

17. PAGER

This variable contains the names of the program used for paging output.

18. PS1

It prompts the contents of the shell, it is short for “prompt string 1”.

19. BROWSER

This variable displays the path to the default web browser.

20. MAIL

This variable displays the path to the current user’s mailbox.

21. LS_COLORS

This variable defined color codes for coloring the output of the ‘ls’ command.

22. HOSTNAME

This variable contains the computer’s hostname.

23. SHELLOPTS

This variable contains options defined with the ‘set’ command.

24. HISTSIZE

This variable contains the number of command history lines allotted for use.

25. HISTFILESIZE

This variable displays the number of lines in command-line history.

26. BASH_VERSION

This variable contains the version of the bash shell used.

Now that we have laid down the fundamentals like what are the environment variables, the format of creating them, and the important environment variables, let us see how to modify, create, set, delete and list the variables using the terminal in Linux-based operating systems.

Listing all the environment variables

If you want to print all of the environment variables, use the command: “printenv”. Since there are a lot of variables present in the system, you can use pipe the command with the less filter as follows: “printenv | less”

listing all the environment variables

Searching for a single environment variable

If you want to check a single environment variable value, use the command:

“printenv <variable name>”

searching for a single environment variable

We can further pipe this command with grep to provide more specific outputs. We can combine them as follows: printenv | grep <variable name>

Searching for single environment variable

Setting an environment variable

If you want to create an environment variable in Linux, you can do so by using the following syntax:

<variable name>=<variable value>

 

But how do you check if the variable has been created or not. You cannot use the “printenv” command as it is useless in this condition.

Therefore to confirm the creation of the variable, we use the following command:

“set | grep <variable_name>”

setting environment variable

Now you can see that the terminal prints the variable we created.

Exporting an environment variable

In the previous output, using the printenv command did not print the variable we created. In order for the command to print our variable, we need to first export it. To export an environment variable we simply use the command:

export <variable name>

exporting a variable

Another way of checking our successful creation and exporting of the variable we created, we can use the bash command to start a child session:

confirming creation by creating a child session of shell

Setting an environment variable permanently

If you want a variable to continue existing even after you kill the shell session, you need to set it permanently. To do so, use the command:

“sudo nano ~/.bashrc”

This command will let you edit the .bashrc file:

setting an environment variable permanently

setting environment variable permanently

In the .bashrc file, Write a line for each variable you wish to add using the following syntax:

export <variable_name>=<variable value>

setting an environment variable

After adding your variable, save and exit the file. The changes you made will be applied after you restart the shell. However, if you are lazy and impatient like me, you can simply type the command “source ~/.bashrc” to apply the changes during the current session itself.

Deleting an environment variable

If you want to delete or unset a permanent variable you can use the command: unset <variable name>

deleting a permanent variable

You can check if the variable has been successfully deleted or not by using the printenv command.

Persistent environment variables

If you want to make environment variables persistent, you need to define those variables in the bash configuration files. In most Linux based operating systems, when you start a new session, the environment variables are read from the following files:

1. /etc/environment – Use this file to set up system-wide environment variables. Variables in this file are set in the following format:

2. /etc/profile – Variables set in this file are loaded whenever a bash login shell is entered.

3. Per-user shell specific configuration files

Summary

In this article, you have learned what environmental variables are, their scope of them, and the rules for defining environmental variables. We also looked at some of the most important environmental variables along with how to list, set, export, and delete an environment variable.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *