×
< BLOG
Hardware & Software

Environment Variables in Linux

February 29, 2016

Guest Author

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

  1. env  – The command lists all of the environment variables in the shell.
  2. printenv – The command prints all (if no environment variable is specified) of environment variables and definitions of the current environment.
  3. set – The command assigns or defines an environment variable.
  4. unset – The command deletes the environment variable. 
  5. 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.

HOSTNAME=”PizzaHeaven”

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.

Edit the .bashrc or ~/.bash_profile configuration files to create persistent environment variables. See the image below as an example of the configuration file in the nano text editor.bashrc configuration file linux

Common Environment Variables

Become familiar with common Linux environment variables by referring to the cheat sheet below (you can save the image).

environment variables linux cheat sheet

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. 

subscribe by email

Stay Ahead