We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

44
1.665.872,45

Cart

Quantity 13.900,05
Quantity 22,00
Quantity 18,00

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.

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

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

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.

44
1.665.872,45

Cart

Quantity 13.900,05
Quantity 22,00
Quantity 18,00