How to Set Environment Variables in Ubuntu/Linux?


In Linux, environment variable refers to any variable that is executable in the specific environment The environment could be the current shell/user or the system as a whole. Environment variables are used to store any path of the executable or just general information. The executable could be a program or a Linux command.

The environment variables are called to run the command or fetch the information from the specific path. For instance, the PATH environment variable contains all the possible executable locations. Whenever any command/executable is run from the terminal, the system looks for the executable locations inside the PATH variable. That’s how important an environment variable could be.

Are you curious to set environment variables on Ubuntu/Linux for your ease? Let’s do it together with this post.

First, start with the naming conventions (recommended):

Naming Convention for the Environment Variables

There are no defined rules when choosing a name for the environment variable. However, there are a few recommendations:

  • Set the environment variable name in UPPER-CASE (recommended as there is no restriction to use lowercase). Lower cases are mostly used by the application’s executables.
  • The environment variable names are case-sensitive, i.e., do remember the name/letter case of the variable.
  • If the variable name is lengthy and you want to split it, then use the “underscores”, i.e., NEW_VER.
  • Do not choose the environment variable name that is already used/defined.
  • Ensure to not use the digit in the name. Or at least do not start with a digit/number. As the applications/processes might get confused when recognizing the variable.

Types of Environment Variables in Ubuntu/Linux

In Ubuntu/Linux, there are two types of environment variables, i.e., local, and global. Local variables are accessible for the current terminal session or for the user session. Whereas the global variables are set system-wide and are accessible to all the users.

Local and Global environment variables are initialized/set differently. These methods are elaborated on separately in the upcoming sections:

How to Set the Local Environment Variables in Ubuntu/Linux

The local variables are declared on the terminal and are accessible only within the shell’s/user’s environment. They are either temporary or permanent. Let’s see how they work:

Set a Temporary Local Environment Variable

Temporary variables are applicable only for the current shell session (applicable for the current user only). The temporary local variables are set on the terminal using the “export” keyword, as follows:

export var-name=var-value

Note: If there is a space in between the variable values, use the single or double quotes.

To print the environment variable on the terminal, use either one of the syntaxes:

printenv Var-Name
echo $Var-Name

Now, if you switch the user or open a new terminal session, you won’t be able to access that variable again. Let’s switch and check the variable’s accessibility:

It is foreseen that the variable is not accessible in other users’ environments.

Set a Permanent Local Variable

The permanent variables (for the current user) are set by modifying the “.bashrc” or the “.profile” files. These files are present in the user’s home directory. When the variable is defined in it, it will be accessible (to the current user only) as long as you delete it from the file. This file could be “.bashrc”, “.bash_profile” or “.profile”. To check which file to choose and why, check out the following description:

  • .bashrc”: It is loaded whenever the new shell session is started (interactive non-login). Keeping environment variables in it means the variable will be loaded again and again, which is not recommended.
  • .bash_profile” or “.profile”: Loaded at the startup of the system (interactive login shell) or any shell that requires login (SSH). It is recommended to add the environment variables in it as it is recognizable by all interactive (login and non-login) shells. The “.bash_profile” is read by the Bash only whereas the “.profile” refers to all the shells.

Editing “.bashrc” File:

Follow the same export command syntax to add an environment variable in the. bashrc file:

Editing “.profile” File:

Similarly, you need to put the variable inside the .profile as we did here:

Once the editing is made, use the source command to reload the files (or refresh the terminal session):

Now, the variables set here are accessible for the current user only irrespective of the reboot, new shell, etc. If you switch the user, you won’t be able to fetch/get these variables.

How to Set the Global Environment Variables in Ubuntu/Linux

As seen in the above example, the variable declared on the terminal is accessible only in the current shell or for the current user. The Global environment variable fulfills this gap and allows you to set the environment variable globally. The variables set in the “/etc/environment” and “/etc/profile” files are known as System-Wide or Persistent environment variables. They are system-wide and each user can use these variables. You have two possibilities here, i.e., adding the environment variable in “/etc/environment” or “/etc/profile”:

Make the Variable Useable For All Users and Processes | “/etc/environment”

If you add the environment variable in the “/etc/environment” file, any user or any process running on the system can access that variable. To do so, open the “/etc/environment” file in any editor and add the environment variable at the end of the file:

Let’s source the file now:

Verify the persistent environment variable:

Make the Variable Useable For Processes Only | “/etc/profile”

The variable put in the “/etc/profile” file is usable for the processes only. Open the file in any editor and put the variable as:

Source the file:

That’s how you can manipulate the “profile” and “bashrc” files to configure the environment variables.

Tip: How to Display/Print the Environment Variables in Ubuntu/Linux

Ubuntu/Linux offers multiple commands to list/view/display the environment variables. The list of commands and their purpose is listed below:

Command Description/Purpose
printenv Displays the Global Environment Variables
set Lists the Global/Local Env Variables
env Shows the list of Global variables
echo $VARIABLE Prints the value of the specific VARIABLE
printenv VARIABLE Displays/Prints the value of the VARIABLE

Troubleshooting-Tip: How to Configure the Path Environment Variable

While working with Linux executables, you might have come across the PATH environment variable. If the system does not locate the executable path to the application/command being run, you won’t be able to use it until you configure the PATH variable.

Let’s first print the PATH variable:

echo $PATH

What if you want to add an executable path to it? Let’s discuss the possibilities:

Configure the PATH Variable Temporarily | User/Shell-Specific

Let’s say you want to make an executable recognizable which is at the location: “/usr/local/exec”. You can simply add this directory to the PATH variable as:

export PATH=$PATH:/usr/local/exec

Note: This temporary addition is only applicable for the current session. Whenever the session is refreshed or the user is switched on, the path will be reset to its previous state.

Configure the PATH Variable Permanently | User-Specific

To make the changes in the PATH variable permanently (but for the current user only), you must add this command to the “.bahsrc” file. Let’s open it and add the executable directory path:

To apply the changes, refresh the terminal session or source the bashrc file.

Configure the PATH Variable Permanently | System-Wide

To make the path variable system-wide recognizable, you need to put that export command in the “/etc/environment” or “/etc/profile” file or just append the path of the directory as shown below.

Now, the PATH variable will be configured for the whole system, irrespective of the shell/user sessions.

How to Unset Environment Variables in Ubuntu/Linux

Until now, we have learned numerous ways to set the variables in Ubuntu/Linux. Let’s learn how to unset the environment variable in the Ubuntu or other Linux distributions. There are two possible ways to unset the environment variables, i.e., using the unset keyword or removing the variable from the respective file.

Using the unset Keyword

The unset keyword unsets the environment variable from the shell environment. This is usually used to unset the variable declared on the Shell. Let’s have a look at its syntax:

unset Variable_Name

Using the Configurations Files

As discussed, the unset keyword only unsets the variable on the shell. However, if the variable is set in any of the configurational files, you have to manually remove it. Let’s say we have a variable named “SITE” which is declared in the “.bashrc” file. We tried to unset it using the unset keyword but it was still accessible, as seen below:

It can be seen that when the .bashrc file is sourced, it again fetches the value. Now, let’s remove it permanently.

Open the “.bashrc” file in any editor:

Erase/Remove the variable (highlighted in the image above) to be unset and then save it. Lastly, the source opens a new terminal session to verify, and the variable will no longer be available.

Bottom Line

Ubuntu/Linux environment variables are categorized into local and global variables. The local variable can be set temporarily and permanently for the current users only. Whereas the system-wide environment variables are applicable to all the users.

Primarily, environment variables are set to launch/initiate any executable program/application. However, they are also helpful in carrying out complex queries in the specific session. You can address those queries with some shorthand word and call it to accomplish the task.

That’s all about today’s guide on environment variables on Ubuntu/Linux.

Print Friendly, PDF & Email
Categories