How To Set and Unset Environment Variables on Linux?

In Linux, the environment variables are considered for the program execution. There are two types of environment variables: one is system defined, and the other one is user-defined. The System defined environment variables give a variety of aspects of the shell, such as the name of the shell stored in the “$SHELL” variable. While the user defines the user-defined variables.

This post will demonstrate the methods to set and unset environments on Linux.

  • How to Set Environment Variables on Linux?
    • Method 1: Through export Command (Bash Shell)
    • Method 2: Through “.bashrc” File
  • How to Unset Environment Variables in Linux?

How to Set Environment Variables on Linux?

The environment variables can be set either temporarily or permanently. The temporary variables can be accessed only in the current shell’s environment, while the permanent variables can also be accessed in new sessions. 

Let’s discuss both methods one by one. 

Method 1: Through the export Command | Temporary Environment Variables 

The “export” is the built-in command of the Bash shell that defines the environment variable’s setting with the following syntax:


$ export [Variable_Name]="Value"
  • The “export” command to export the variable.
  • Define the “Variable_Name” to export it.
  • Define the “value” of the variable.

Example: Set Environment Variables

To set the environment variable named “variable” using the export command, the following command is examined:

$ export variable="98"

The environment variable “variable” is set with the value “98”.

Let’s check the value of the defined variable through the “echo” command in the bash shell:

The environment variable “variable” is printing the value of “98”.

Default Environment Variables

As mentioned earlier, Linux has some default variables such as “$PWD,” “$HOME,” and “HOSTNAME.” To print it using the echo command is obtained as follows:

$ echo $PWD
$ echo $HOME
$ echo $HOSTNAME

The variable “$PWD” holds the current directory address, and the “$HOME” variable stores the address of the home directory. While the “$HOSTNAME” contains the hostname of the system.

Note: The variables are temporary and only available in the current bash shell.

Method 2: Through “.bashrc” File | Permanent Environment Variables

The second method to set the environment variables is using the “.bashrc” file, which sets the environment variable permanently. To perform this method, go through the given steps.

Step 1: Define Environment Variable in “.bashrc” File

Open the “.bashrc” file with the nano editor and define the environment variables using the export command. 

$ nano .bashrc                               #Open the File With Nano Editor

Once the file is opened, you must add the environment variable as per the following syntax: 

$ export name="Henry"

Save the file and exit it by pressing the “Ctrl+X.”

Step 2: Execute the “.bashrc” File

To use the defined environment variable, re-execute the “.bashrc” file through the source command to see the new changes:

$ source .bashrc

The “.bashrc” file is executed.

Verify the Change 

Verify the environment variable’s value by printing it on the screen through echo statement:

$ echo $name

The variable “$name” has the value of “Henry,” as given in the above steps.

How to Unset Environment Variables in Linux?

The following command syntax is utilized to unset all the temporary and permanent environment variables:


$ unset <variable name>
  • The “unset” command to unset the variable.
  • Define the “Variable_Name” to unset it.

If the “unset” command is executed using the above syntax, all the user-defined variables will be unset as shown:

$ unset name
$ echo $name

The variable “$name” is unset, and there is nothing to print.


To set the environment variables in Linux, use the export command with the syntax “export [Variable_Name]=”Value” while to unset it, use the syntax “unset name.” Using the “export” command in the shell is set the temporary variables while defining variables in the “.bashrc” file is set permanently. This write-up has illustrated the methods to set and unset the environment variables in Linux.