How-To

How to Set Environment Variables in Linux

Linux featured

As you customize and configure your Linux system, you’ll need to know how to set environment variables. Let us walk you through it.

If you’re using a PC, the operating system needs to know where to locate certain files or how to present the time and date. Windows typically does this using configuration files and the registry.

On Unix-based systems like Linux, this is set using environment variables. Knowing how to set environment variables in Linux can be a big help, and this guide will help you get started.

Environment and Shell Variables in Linux

There are two types of these variables—environment and shell. The environment variable affects the system as a whole, whether on the command line or in a graphical interface. These variables store things like the default text editor or web browser, the path to executable files, or the system locale and keyboard layout settings.

A shell variable, on the other hand, has a more limited scope. It’s non-persistent, applying only to the current shell (or command line) instance. The different shell environments, like zsh and bash, have their own set of internal shell variables.

What both have in common is the format. Every environment and shell variable will follow one of these basic formats:

KEY=value
KEY="Some value including spaces"
KEY=value1:value2

Keep the following requirements in mind when working with environment and shell variables:

  • Case matters. Environment variables should always have upper case names.
  • Never include a space between the variable name, the = symbol, and the value.
  • When a variable has multiple values, they must be separated by the colon (:) character.

How to List and Set Environment Variables in Linux

Several commands are available for listing, setting, and unsetting environment variables in Linux. These include:

  • env: This command will allow you to run another program in a custom environment without changing the current one. Without any arguments, it prints a list of the current environment variables.
  • printenv: If you want to print all or a specific environment variable, printenv is quite useful.
  • set: This command is used to set or unset shell variables. If you run it without an argument, set will print a list of all variables, including environment and shell variables as well as shell functions.
  • unset: The command to delete shell and environment variables.
  • export: This is the command you’ll most commonly use to set environment variables.

Working With Environment Variables

Before going about changing environment variables, you should always check your current configurations. Using printenv, you can find out what environment and shell variables your Linux system already has.

To see all the current environment variables, run the printenv command without any arguments. Warning, the output will be rather lengthy.

For example, to display the value of the HOME variable, you’d use the following.

jeff@UbuntuVM:~$ printenv HOME
/home/jeff
jeff@UbuntuVM:~$

You can also provide more than one argument to printenv, to check multiple variables at once.

jeff@UbuntuVM:~/documents$ printenv LANG PWD
en_US
/home/jeff/documents
jeff@UbuntuVM:~/documents$

Some of the most common environment variables you’ll encounter are:

  • USER: The current logged-in user.
  • HOME: The home directory of the current user.
  • EDITOR: The default text editor used if you type edit at the command line.
  • SHELL: The path of the current user’s shell, like bash or zsh.
  • PATH: A list of directories to be searched when you execute a command. Linux searches these directories in the specified order, using the first match it finds.

Changing or Setting New Environment Variables

There are two steps you’ll need to follow to set an environment variable on Linux. First, you define it as a shell variable. Next, you’ll export it as an environment variable. For example, this command sets a variable named MY_VAR and defines it as groovy.

jeff@UbuntuVM:~$ MY_VAR='groovy'
jeff@UbuntuVM:~$ 

You won’t see any confirmation of this, but you can check if it was actually set.

jeff@UbuntuVM:~$ echo $MY_VAR
groovy
jeff@UbuntuVM:~$ 

Issuing the command printenv MY_VAR at this point shouldn’t return any output, showing there isn’t an environment variable named MY_VAR. To change that, you’d use the export command.

jeff@UbuntuVM:~$ export MY_VAR
jeff@UbuntuVM:~$ 

Now, printenv MY_VAR should have different output.

jeff@UbuntuVM:~$ printenv MY_VAR
groovy
jeff@UbuntuVM:~$ 

Of course, there is another way to accomplish this. You can set environment variables in a single line using a command like this one:

jeff@UbuntuVM:~$ export MY_NEW_VAR="Another groovy variable"
jeff@UbuntuVM:~$ 

One thing to note is that environment variables set this way only apply to the current session. Once you log out or open a new shell, the environment variables are lost.

Setting Environment Variables to be Persistent in Linux

If you know you’ll need to use environment variables persist across logins, you’ll need to define them within your bash configuration files. These are found in a few places.

  • /etc/environment: This file sets up global, system-wide environment variables that apply to all shells and the GUI environment. These are set in the following format:
    FOO=bar
    VAR_TEST=“Test Variable”
    
  • /etc/profile: These are loaded whenever a bash login shell is entered. Use the export command to declare these.
    export JAVA_HOME=“/path/to/java/home”
    
  • User-defined shell configuration files: Every user can have its own configuration files for the shell. For bash, the file is located at ~/.bashrc. Declare variables here the same way you would in /etc/profile.
    export PATH=“$HOME/bin:$PATH”
    

If you’ve set new environment variables and want to quickly load them in your current shell session, you don’t have to log out. Just use the source command.

jeff@UbuntuVM:~$ source ~/.bashrc
jeff@UbuntuVM:~$ 

Configuring Shell Variables

You may not need to declare these as often as environment variables, but the set command is used for setting and listing all shell variables. If you want to list all of them, just issue the command without any arguments.

jeff@UbuntuVM:~$ set

typical output of set command

The output will be many pages long, probably. To make it more readable, pipe it through the less command:

jeff@UbuntuVM:~$ set | less

typical output of set command piped through less

If you want to print a specific shell variable, you can also use the echo command.

jeff@UbuntuVM:~$ echo $BASH_VERSION
5.0.17(1)-release
jeff@UbuntuVM:~$ 

Setting a shell variable is as simple as typing the variable name, the = symbol, and the value.

jeff@UbuntuVM:~$ MY_VAR="groovy"
jeff@UbuntuVM:~$ 

Administer Your Linux Desktop or Server Wisely

Setting shell and environment variables can be a powerful capability on a Linux PC or server, but be cautious about how you go about it. Always double-check what you’re typing before you commit to it. When changing files like /etc/profile, you may want to create a backup file first.

For user-level changes, knowing what configuration files are in use helps, as well as where to find them. If you’re struggling to locate a particular file, Linux does include some powerful tools for searching your hard drives.

Click to comment

Leave a Reply

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

 

To Top