Hardware & Software
Environment Variables in Linux
February 29, 2016
An Introduction to Linux Environment Variables and Why They’re Useful
As you hunker down and prep for the CompTIA Linux+ certification exam, refer to this blog post and downloadable cheat sheet that explains common environment variables within Linux, relevant commands, and configuration options.
What are Environment Variables in Linux?
Linux environment variables act as placeholders for information stored within the system that passes data to programs launched in shells or subshells.
Why are Environment Variables Valuable for System Administration?
Admins have the ability to modify environment variables to fit personal or larger group needs of users within their environments. As you’ll notice below, admins can alter the hostname, command-line prompt, coloring in shells for text, and various other environment variables to better suit user preference.
Commands for Environment Variables
- env – The command lists all of the environment variables in the shell.
- printenv – The command prints all (if no environment variable is specified) of environment variables and definitions of the current environment.
- set – The command assigns or defines an environment variable.
- unset – The command deletes the environment variable.
- export – The command exports the value of the newly assigned environment variable.
How to Define Environment Variables
After seeing the list of present environment variables on your system, you can modify or redefine them. Use the variable’s name, an equals sign (=), and enclose the new definition in double quotes (“”). See the example below.
Then employ the export command with the variable name to export the data to new programs or subshells for use.
Pay Attention to the Dollar Sign ($)
On the command-line and in scripts, the dollar sign ($) precedes environment variables. When redefining variables, do not use the dollar sign.
Persistent and Nonpersistent Environment Variables
When modifying environment variables in your current shell, those variables remain nonpersistent. The changes stay temporary and vanish once you log out of the shell.
You can modify the variables to stay persistent by editing the bash configuration files. After you logout of the current shell, those changes remain intact and permanent for the user(s) or groups.
Common Environment Variables
Become familiar with common Linux environment variables by referring to the cheat sheet below (you can save the image).
Practice Modifying Environment Variables
To prep for the CompTIA Linux+ exam, review the above cheat sheet to increase your familiarity with the environment variables. Repetition of review material increases learning retention. Also, practice changing environment variables and employing the above commands. Command-line practice provides the best learning tool.