How To Set Environment Variables in Linux

Environment variables are important parameters for proper functioning of almost every application. They allow you to customize how your operating system runs various applications. In this article, we will look at how to set environment variables in Linux.

How To Set Environment Variables in Linux

Here is the command to set Environment variables and Shell variables

export KEY=value 
export KEY="Some other value" 
export KEY=value1:value2

The first two statements are used to assign a single value to environment variable. The third statement is used to assign multiple values to environment variable.

For example, here is the command to set environment variable EDITOR to use vi text editor all the time. You can use it to environment variable in bash and other shells.

$ export EDITOR=vi

Now when you run commands like crontab -e then linux will your cronjob file in vi editor instead of using default nano editor.

You can view environment variable value using echo command. However, while using echo command, prefix a $ sign to your environment variable.

$ echo $EDITOR

Also read : How to Calculate CPU Utilization in Linux

Commands for Environment Variables

Here are some of the most common commands to work with environment variables.

  • set – Sets or unsets shell variables. It will print a list of all variables including environment and shell variables, and shell functions, when used without an argument
  • env – run another program in a custom environment without changing the current one. It will print a list of the current environment variables when used without an argument.
  • printenv – Prints all or the specified environment variables
  • unset – Deletes shell and environment variables
  • export – Sets environment variables

Also read : How to Reduce Server Load in Linux

List all environment variables

Here is the command to list all environment variables.


Here is a sample output


Also read : How to Kill Process by Name in Linux

Persistent Environment Variables

If you want to permanently set environment variable for a user, you need to make environment variables persistent.

For this purpose, you need to store these environment variables in a system file that is used by Linux to read environment variables, in every session. Here are three files from which environment variables are read every time a user starts a new session.

  1. /etc/environment – Variables in this file are system-wide environment variables. You can set them in following format. There is no need to specify EXPORT command.

2. /etc/profile – Variables in this file are loaded whenever a user enters bash shell. Here is the format to set environment variables in this file, using EXPORT command.

export JAVA_HOME="/path/to/java/home"

3. ~/.bashrc – This the per user shell specific configuration file that is used to read all environment variables for each user. You can use the source command to open this file.

source ~/.bashrc

and use EXPORT command to set environment variable in this file.

export JAVA_HOME="/path/to/java/home"

Leave a Reply

Your email address will not be published. Required fields are marked *