Google Cloud Linux

How to Set, List and Remove Environment Variables in Linux

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.

Best Hosting for your Business

PlatformReviewsPricing
Cloudways★★★★★$10
Kinsta – Google Cloud★★★★★$30

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.

KEY1=value
KEY2="value 2"
KEY3=value1:value2
  • 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.

env
printenv

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
Output
/home/cloudbooklet

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

printenv TERM SHELL
Output
xterm-256color
/bin/bash

Using echo command.

echo $HOME
Output
/home/cloudbooklet

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

echo $TERM $SHELL
Output
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

Conclusion

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.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

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