Skip to content

Environment Variables: What They Are and How to Use Them

26 October 2022
Environment Variables

Environment Variables are a powerful tool for managing configuration settings in software development. Learn what they are and how to use them in this blog post.

Environment Vairables

  • Bash variables are local to a single shell by default
    • Set with VARIABLE=VALUE
  • Environment variables are inherited by child shells
    • Set with export VARIABLE=VALUE
    • Accessed by some programs for configuration

The use of variables is central to the process of configuration, whether configuration the shell or some other program.

A variable is a label that equates to some value. The value can change over time, across system, or across accounts, but the label remains constant. For example, a shell script may place a file in $HOME, a reference to the value of the variable HOME. This value may differ, depending on who is running the shell script.

Some variables, like ${HOME], are intended to be referenced but not set by the user. However, users can also create their own variables:

[user@stationX ~]$ HI=”Hello, pleased to meet you.”
[user@stationX ~]$ echo ${HI}
Hello, pleased to meet you.

When setting variables at the command line, do not include spaces between the variable, the equals sign and the value. Entering something like HI = “Hello, pleased to meet you “ would probably generate a syntax error because bash will try to execute a command called HI, which is unlikely to exist.

Two types of variables exist: local variables, also called shell variables, and environment variables. The difference between the two is that the shell will pass the environment variables on to commands that it calls, but it will not pass on local variables. As a result, local variables are used to configure the shell itself, while environment variables are used to configure other commands.

The set, env, and echo commands can be used to display all variables, environment variables, and a single variable value, respectively. Eexamples:

[user@stationX ~]$ set | less
[user@stationX ~]$ env| less
[user@stationX ~]$ echo ${HOME}

/Home/user