What are environment variables in Bash? – Opensource.com

In computing, a variable is a word that represents a value that can change. You use variables every day in normal speech, although you don’t think of them as variables. When you say “my car,” you’re using “my car” as a kind of variable that refers to whatever car you have at the time. The make and model will surely change throughout your lifetime as you replace an old car with a new one, but by creating the variable “my car,” you don’t have to ramble on about what type of car you currently own every time you want to refer to your vehicle.

In computers, variables are used in a similar way.

For example, anyone with a user account on a computer has a home directory where the computer saves their personal data. But each user has a distinct home directory (/home/seth for Seth, /home/tux for Tux, and so on for each user), so when you want to refer generically to a home directory, you can use the $HOME variable to indicate the current user’s home directory, regardless of the user’s login name.

Environment variables

are special variables (such as $HOME) that contain information about your logon session. They’ are stored for the system shell to use when executing commands. They exist whether you’re using Linux, Mac, or Windows. Many of these variables are set by default during installation or user creation.

While environment variables

apply to all modern systems, this article specifically addresses environment variables in the Bash shell on Linux, BSD, Mac, and Cygwin.

Understanding

Environment Variables Environment variables are not technically different from variables. They can be configured, retrieved and deleted with exactly the same syntax used for variables. If you’re not used to using variables in Bash, read my article on variables in Bash before continuing.

You don’t typically use environment variables directly. They are referenced by individual applications and daemons as needed. For example, your home directory is set as an environment variable when you log in. For example, on Linux you can view the contents of your HOME environment variable like this

: $ echo $HOME HOME=/home/seth

On a Mac

: $ echo $HOME HOME=/Users/bogus

On Windows

: C:\Users\bogus

You can view all environment variables set on your system with the env command. The list is long, so channel the output more to make it easier to read:

$ env | more TERM=xterm-256color LESSOPEN=|| /usr/bin/lesspipe.sh %s USER=seth SUDO_EDITOR=emacs WWW_HOME=http://mirror.lagoon.nc/pub/slackware/slackware64-current/ChangeLog.txt VISUAL=emacs DISPLAY=:0 PS1=$ XDG_DATA_DIRS=/home/seth/.local/share/flatpak/exports/share/:/var/lib/flatpak/exports/share/:/usr/local/share/:/usr/share/ PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/snap/bin:/home/seth/bin:/home/seth/.local/bin:/snap/bin GDMSESSION=gnome MAIL=/var/spool/mail/seth […]

Environment variables can be useful when you want to override default settings or when you need to manage new settings that the system has no reason to create on its own. For example, when you type a command, the only reason your computer knows how to find the application corresponding to that command is that the PATH environment variable tells you where to look. This variable lists valid directories for your operating system to search for commands, whether the command is ls or cp, or a graphical application like Firefox or Lutris, or anything else.

Different environment variables are used by different systems. Its PATH variable is vital for your terminal emulator, for example, but much less significant for, say, Java (which has its own paths, pointing to important Java libraries). However, the USER variable is used by various systems as a way to identify who is requesting a service. For example, if you are on a multi-user system and need to check your local mailbox, your mail command knows which mail queue to retrieve based on the MAIL and USER variables.

Setting

an environment variable

Usually, the installer program, whether dnf in Fedora, apt in Ubuntu, brew in Mac, or a custom installer, updates its environment variables for a new application. Sometimes, however, when you’re installing something outside of your distribution’s intended toolset, you may have to manage an environment variable yourself. Or you can choose to add an environment variable that suits your preferences. If you decide that you want to keep some applications in a bin folder located in your home directory, you should add that directory to your PATH so that your operating system knows to look there for applications that will run when you issue a command.

Temporary environment

variables

You can add a location to your path in the same way that you create disposable variables. It works, but only as long as the shell you used to modify the system path remains open. For example, open a Bash shell and modify the system path

: $ export PATH=$PATH:/home/seth/bin

Confirm the result

: $ echo $PATH PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/bin:/usr/sbin:/snap/bin:/home/seth/bin:/home/seth/.local/bin:/snap/bin:/home/seth/bin

Logout

: $ exit Open a

new one and take a look at the PATH variable:

$ echo $PATH PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/snap/bin:/home/seth/bin:/home/seth/.local/bin:/snap/bin

This variable has reverted to its default state because PATH is not set with each new shell. For that, you need to set your variables to load every time a shell is started.

Permanent

environment variables

You can set your own persistent environment variables in the shell configuration file, the most common of which is ~/.bashrc. If you are a system administrator who manages multiple users, you can also set environment variables in a script placed in the /etc/profile.d directory.

The syntax for setting a variable per configuration file is the same as setting a variable in your

shell: export PATH=$PATH:/snap/bin:/home/seth/bin Close the

current shell or force it to load the updated configuration:

$ . ~/.bashrc

Finally, take another look at

your system path: $ echo $PATH PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/snap/bin:/home/seth/bin:/home/seth/.local/bin:/snap/bin:/home/seth/bin

It is now properly configured to include your additional custom directory.

Discovery of other environment variables You can create and manipulate

environment variables

at will, and some applications do just that. This fact means that many of your environment variables are not used by most of your applications, and if you add your own arbitrary variables, some could be used by nothing at all. So the question is: How do you figure out which environment variables are significant?

The answer lies in an application’s documentation. For example, to find out what options are available for your overall Bash environment, you can read the Bash documentation. While the Bash man page mentions many important variables, the GNU information page for Bash presents two exhaustive lists of useful Bourne Shell and Bash environment variables, and how each is used.

For example, in the list on the information page: ‘HISTCONTROL’ A list of values separated by a colon that controls how commands are saved

in the history list

. If the value list includes ‘ignorespace’, lines that begin with a space character are not saved in the history list. A value of ‘ignoredups’ causes lines that match the previous history entry not to be saved. A value of ‘ignoreboth’ is short for ‘ignorespace’ and ‘ignoredups’. […]

This output tells you that the HISTCONTROL environment variable controls how your Bash history is presented and what values you can use to customize that experience. In this example, the value ignoredups instructs the output of the history command to ignore duplicate lines.

You can try this one easily. First, issue the same command twice in a row

: $ echo “hello world” hello world $ echo “hello world” hello world

View your history, or at least the most recent entries: $ history

| tail -5 996 man bash 997 info bash 998 echo “hello world” 999 echo “hello world” 1000 story

You can see that duplicate entries are listed now

. Set a new environment variable in your .bashrc

file based on what you read on the information page: export HISTCONTROL=$HISTCONTROL:ignorespace:ignoredups

Save and load the new settings

: $ source ~/.bashrc

Issue two commands twice in a row

: $ echo “hello once” hello eleven $ echo “hello once”

hello once See the most recent entries in your history:

$ history | queue -5 1000 history 1001 emacs ~/.bashrc 1002 source ~/.bashrc 1003 echo “hello once” 1004 history

Duplicate entries are now collapsed into an entry due to the new environment variable, as specified on the information page

.

Finding relevant environment variables is often a matter of reading the documentation for the application you want to affect. Most environment variables are specific to what an application needs to run smoothly. For general entries, the shell documentation is the logical place to look. If you write scripts or applications that require new environment variables, be sure to define those variables in your own documentation.