Linux Set Environment Variable Command Line

We mainly encounter variables when writing a code. However, even in your operating system, you can set the environment variables that you can use in different instances. In Linux, each user has specific environment variables. Moreover, there are other global variables accessible to all users.

You can use the environment variables to set a variable that only you can access. How do you set the environment variables in Linux? This post will guide you.

Understanding the Linux Environment Variables

By default, Linux has some defined environment variables that you can invoke to access a given item. For instance, if you want to check the currently logged-in user quickly, you can access the “$USER” environment variable.

You can echo it or use other options to access it. Take a look at the following example:

There are numerous environment variables in your Linux system. You can list them using the following “env” command:

Once you execute the command, you will get an output of the different environment variables that are currently available on your system. The listed environment variables contain those temporarily and permanently set for the specific user.

Note that using “env” only lists the environment variables but doesn’t give the details for the listed environment variables. To get the values for each environment variable, you can use the “printenv” command and specify that variable’s name.

For instance, if we want to access the SESSION_MANAGER, we run our command as follows:


The output displays the value that is contained by the particular environment variable.

Alternatively, you can use the “echo” command to get the value of a given environment variable. The only drawback is that the “echo” command may sometimes be ineffective. Nonetheless, here’s how to use it to get the same output:

Set the Environment Variable on the Command Line in Linux

So far, we understood what an environment variable is and seen how you can list all the environment variables and access their values. Let’s now see how to define and set your environment variable on the command line.

Here’s the syntax to set the environment variables on the command line:

$ export VARIABLE_NAME=value

We can break the syntax as follows:

First, let’s create our variable and give it a value. Our variable is “LINUX_HINT” and its value is “demo1”.

If we echo this environment variable, we will manage to get its value.

However, using the “printenv” command to get its value won’t work as expected. This scenario happens because we have not exported the created environment variable to make it fully accessible by the logged user.

So, export the environment variable as shown in the following:

Once you exported the environment variable, it will appear when you use the “printenv” command. Moreover, it appears when you list all the available environment variables on your Linux system.

The method we used to set the environment variable is long. We can quickly set a Linux environment variable using one command.

For instance, if we were to create or update a new environment variable if it already exists, we can run our command as follows:

$ export LINUX_HINT=’we have changed it’

With this option, once you run the “printenv” command, it gives the output of that environment variable.

How to Unset an Environment Variable

Imagine a case where you no longer want to have the set environment variable on your system. Since the set environment variable is temporary until the next reboot, you may choose to restart your system. Alternatively, you can use the “unset” command.

Once you run the “unset” command and specify the target environment variable, you have removed it.

How to Permanently Set the Environment Variables

If you want to set an environment variable permanently, you must modify the “.bashrc” file and add the environment variable.

Open the “.bashrc” file with a text editor.

Once it opens, scroll to the bottom of the file and add the export line with the variable name and its value.

Save and exit the file. Next, we must run the “source” command to implement the changes.

That’s it! Even when you restart your Linux, the environment variable will persist and you can verify so using the “printenv” command.


To set the environment variables in Linux, we use the “export” command. Moreover, you can modify the “.bashrc” file to set the environment variable permanently. We discussed how you can implement both instances. Try them out!


Leave a Comment