Google Cloud Linux

How to Set, List and Remove Environment Variables in Linux

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

Environment variables are key-value pair in Linux which are stored permanently or temporarily to be used by applications through shell.

In this guide you are going to learn how to setup environment variables in Linux, list them and remove them.

The global environment variables are stored in etc/environment. Any changes that is made in this file reflects throughout the system for all users.

Set Temporary Environment Variables

Temporary variables are only available to the current shell session. The variables will get deleted once you close the terminal.

You can create temporary variables using the following syntax.

KEY2="value 2"
  • The environment variable names should be in UPPERCASE. They are case sensitive.
  • The name and value pair should be separated by = sign without any spaces around it.
  • Multiple values can be added to a single variable which is separated using colon:.
  • The values that are having spaces should be enclosed using quotes " ".

List Environment Variables

You can list all available environment variables by simply using the following commands.


These both commands will list all the environment variables stored in the system.

Read Environment Variables

You can read the environment variables using the printenv command and echo command.

Using printenv command.

printenv HOME

You can also use the printenv command to read multiple variables.

printenv TERM SHELL

Using echo command.

echo $HOME

You can also use the echo command to read multiple variables.

xterm-256color /bin/bash

Delete Environment Variables

It is very simple to delete environment variables. You need to just use the unset command with the variable name to delete it.

unset variablename

This command will remove the variable permanently.

Set Permanent Environment Variables

The permanent environment variables are stored at 3 places.

  • /etc/environemnt: This file stores the variables that are globally accessible by all users throughout the system.
  • /etc/profile: Whenever a bash shell in entered the variables in this file will gets loaded. To add environment variable to this file you need to use the export command.
  • ~/.bashrc: User specific environment variables are added here.

To load the added variables in your current session you need to use the source command.

source ~/.bashrc

Prepare yourself for a role working as an Information Technology Professional with Linux operating system


Now you have learned how to set environment variables, list them and remove if not needed.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.