If you've been using Linux for a while, you may be wondering how to set certain parameters from the command line that can keep your settings across programs. Environment variables are how you do this.

By the end, you'll have a deep understanding of what environment variables are, and how you can create such variables from the command line.

What Are Environment Variables?

Environment variables are variables meant to be consistent across all instances of the Linux shell. When you launch a program from the shell, it makes a copy of itself, or "forks," and then replaces itself with the program it's going to run, known as "exec."

Environment variables will be inherited in any copies of the shell that the shell spawns, also known as "exporting" variables. Even Windows uses environment variables, going back to the days of MS-DOS, but in modern times, most programs rely on the Registry and their own settings menus for configuration.

An environment variable might define your favorite editor, so when a program launches the text editor to change a configuration file, this editor will come up every time.

Environment variables can be set system-wide by the system administrator, but it's more common for them to be defined in startup files for individual users. By convention, they're printed in upper case and are indicated by a leading "$" sign, such as "$EDITOR" for the default text editor.

Listing Environment Variables

To see the value of any environment variable, use the echo command. For example, to view the value of the variable $EDITOR, run the following command:

        echo $EDITOR
    

If it's set, the shell will print the value, such as "vim", but if it's empty, it will just print a blank line.

To see all the currently set environment variables, type "set" at the command line.

running the set command

Setting Environment Variables on Linux

You can set environment variables in two ways: from the command line or in shell configuration files.

The first method is simple. In Bash, you use the "export" command. For example, to set the $EDITOR environment variable:

        export EDITOR='vim'
    

Note that in this syntax, you omit the leading "$". You can also use VARIABLE='value', but that will just extend to that particular instance of the shell, meaning the shell will reset the changed value once you close the terminal.

Now your editor will be Vim or whatever text editor you have in this session and every subshell it launches. If you want to keep your environment variables among different shell sessions, define them in your shell's startup files.

Bash reads several files on startup: /etc/profile, /etc/bash/bashrc, .bash_profile, and .bashrc. The first two are system-wide and only a system administrator can modify those, while the rest reside in your home directory.

The .bashrc file is what you want to edit, as it affects interactive shells, whether it's running as a login shell or not. If you're using the shell from a terminal emulator, .bashrc is what will get read.

The method of setting environment variables in a file is the same as setting it at the command line. Just append the export VARIABLE='value' command to the shell configuration file. When you're finished, save the file and launch a new shell.

Note that Linux users can also change the default shell from Bash to some other shell. For those who have, you'll have to edit your shell's configuration file instead of .bashrc, which is the config file for Bash.

Now You Know How Environment Variables Work

With environment variables, you can have consistent settings across all your shell sessions. Shell variables are an important part of shell scripting, something every Linux user should know about.