Tuesday, April 30, 2024
HomeTutorialsHow To Set and List Environmental Variables in Linux

How To Set and List Environmental Variables in Linux

In computing, the term environment means a platform with an operating system and a processor where a user can run applications. And variables are like small boxes in computer memory that hold information. The computer assigns and declares a certain amount of memory to each variable. Each variable has its memory address. In other words, a variable is a location for storing values like files, numbers, strings, or any other data. When we need the value, we can recall it by the name of that variable. In Linux, the term environmental variables refer to a set of values with a paired name or alias that functions in a specific application or program environment.

Linux system admins need to know the environmental variables to maintain the applications and programs. Knowing how they are used in different applications and how to list and modify them, admins can control the system as they want.

Environmental Variables in Linux


Mostly in shell scripting, programming, or shell commands, the variable stores the values for a specific character. Variables play an important role in programming as they allow programmers to write flexible programs.

The environmental variables are a set of dynamic values which affect the processes of an application or program. You can create, modify, and remove environmental variables on a Linux system. In this post, we will see how to list and get started with the Environmental Variables in Linux.

1. Basics of Environmental Variables in Linux


Before we jump into the tutorial and how to section, the basic syntax rules and the variable writing rules are here. You can go through these sections to gather a better idea about the environmental variables in Linux.

Here is the set of environmental variables in their standard format.

KEY=value
KEY="NewVALUE"
KEY=valueA:valueB

i) Rules To Write Environmental Variables in Linux


Here, you can see the list of rules that you need to know while writing the environmental variables in Linux.

  • The “=” symbol should not be preceded or followed by a space.
  • White space should be enclosed in quotation marks.
  • Each variable can take on more than one value.
  • Colon (:) characters must be used to separate each value.
  • The environmental variables’ names are case-sensitive by default.
  • Always use uppercase variables when writing environment variables.

ii) Syntaxes of Environmental Variables in Linux


Here we can now see a list of syntaxes of the environmental variables in Linux. What the syntaxes define are listed below. There are some commonly used environment variables on Linux are listed below.

  • Path = The path is a common environmental variable that has a colon (:) in it. It can locate applications and programs.
  • LAN or LANG = The syntax LAN/LANG is used to mention the language.
  • MAIL = The MAIL term is used to denote the directory location on which the mail will be stored.
  • USER = The user is referred to as the username or the hostname on a Linux system.
  • LOGNAME = The log name command (syntax) is also used to see the current user on the system.
  • Home = In environmental variables, the home refers to the Home directory on the Linux machine.
  • UID = The UID is the user ID.
  • TERM = Here, the term is written as the short and symbolic form of the terminal shell.
  • ENV = Lastly, the env refers to the variable that shows all environment variables.

2. List Environment Variables


So far, we have seen the notion of what environmental variables are and where we use them. Here we will see the most used and most common list of the environmental variables on Linux. These variables are mostly used in command-line-based applications such as terminal, shell, and script.

i) Printenv: Environment Variables 


The Printenv is the short form of the original syntax print environment. This tool basically prints all the connection details, user name, user type, directory address, and other details of the system you’re using. The printenv variable works with both arguments and no arguments. If there are no arguments with the syntax, it prints all the available variables on the environment.

$ printenv HOME

If we need to execute more than one printenv command at a time, we can arrange the commands in the shell. Here, the below command will run both LANG (Language) & PWD (current working directory path) on the terminal shell of the system environment.

printenv environment variable in Linux

$ printenv LANG PWD

As I have already said that we can execute the printenv commands with or without arguments. Here, we will see what happens when we don’t put the arguments (parameters) on the command.

$ printenv

You can see that the above command has printed the list of all available environment variables in the system. I’ve added the output of this command for a better understanding of what type of variables does the printenv prints without an argument.

printenv in linux

ii) Set: Environment Variable in Linux 


In Linux, the variables have a few more varieties, such as shell, bash, pwd, MAIL, etc. If we need to print all the available variables with the environmental variables, we can simply execute the set command on the shell.

$ set

Obviously, you would want to pipe the output to the less command if you want to see a detailed list of all variables displayed.

Environmental Variables in Linux

$ set | less

In the same way, we can use echo commands to print specific types of variables on the Linux system.

$ echo $BASH_VERSION

3. Shell Variables vs. Environment Variables


By default, Linux uses the preset variables for running applications and tools. If we need, we can set custom environment variables according to our needs. Many newbie users get confused with the notion of shell and environmental variables. Here, we will also try to clear that doubt in this step.

If we run the below-mentioned command in the terminal for showing the output of a given variable, that would be a shell variable. Here, we assign an argument with a string value, which makes a perfect shell variable.

$ MY_VAR='ubuntupit'

Now, to verify the shell variable, we can execute the echo command or the grep command to sort and filter the variable data.

$ set | grep MY_VAR:
$ echo $MY_VAR

Finally, we can now execute the variable command to check whether the previous variable is an environmental or shell variable. We can go with the basic printenv command on the shell.

$ printenv MY_VAR

We can also try to print the below command in a new shell for being oversure. If we get an empty output, that will ensure that the variable was not an environmental variable; it was a shell variable.

Shell Variables vs Environment Variables Environmental Variables in Linux

$ bash -c 'echo $MY_VAR'

Now, if we want, we can create or convert a shell variable into an environmental variable by simply exporting the shell variable as an environmental variable.

$ export MY_VAR

We can now again run the printenv command; this time, we will get a string value assigned earlier. And, this time, running in a new shell won’t return us to an empty screen.

$ bash -c 'echo $MY_VAR'

4. Run Variable Parameters in a Permanent Way


Till now, we have seen how to declare the environmental variables in Linux. What if we want to set the variables just once and load them every time we fire up the system.

To run the environmental variables in a permanent way in Linux so that you don’t need to define them every time, you will need to edit the bash file and make the desired changes that you want to load. All the environmental variables in Linux will be stored inside the /etc/environment and etc/profile directory.

Here, we will see the function of these directories in order to handle the environmental variables in Linux and how we can modify the values.

i) Directory: /etc/environment


The Linux systems store the logs of the environment variables in the /etc/environment directory. We can modify this directory and make changes inside the script with root access. Here is the sample format of how we can write variables in scripts for this directory.

$ FOO=bar
$ VAR_TEST="Test Var"

ii) Directory: /etc/profile


Most of us are familiar with this /etc/profile directory on Linux, this folder stores the user credentials and log data. This directory also logs the bash data and the data when defining an environmental variable. Earlier, we have seen how to use the export command to make environmental variables in Linux. We can also use the export commands in this directory to make changes inside the scripts.

$ export JAVA_HOME="/path/to/java/home"
$ export PATH=$PATH:$JAVA_HOME/bin

iii) Path: ~/.bashrc


In Linux and other Unix-like operating systems, the bash file controls what to display and run on the system startup and how the specific application will appear. We also know the bashrc file also holds the shell configurations and initializes the shell operations.

We can make changes inside the bashrc file and set some environmental variables in Linux for running the bash according to our needs. The below-mentioned command will allow us to set environmental variables inside the bashrc file.

$ export PATH="$HOME/bin:$PATH"

After you finish editing the bash configuration script, save it and reload the bashrc files on the Linux system to see the impact of the environmental variables in your Linux system.

$ source ~/.bashrc

5. Remove Environmental Variables in Linux


So far, we have seen the setting process of the environmental variables in Linux. Here we will see how to remove them from your system. Removing the environmental variables is easy. We can use the unset command for removing variables. Just make sure that you are on the right directory and using the correct name of the variable you need to remove.

$ unset variable_name

Ending Words


In Linux, you can control how your system functions and the application’s behavior by using and customizing the environment variables. In the entire post, we have seen the notion of environmental variables in Linux and how we can export, change and modify them in different directories. We have also seen the main difference between the shell and environmental variables.

Please share it with your friends and the Linux community if you find this post useful and informative. We also encourage you to write down your opinions regarding this post in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

You May Like It!

Trending Now