The Power of Linux Shell Environment Variables

The Power of Linux Shell Environment Variables

If you're working in a Linux environment, chances are you've encountered environment variables—even if you didn’t realize it at the time. They quietly power much of what goes on behind the scenes in your shell sessions, influencing everything from what shell prompt you see to which programs are available when you type a command. Whether you're an experienced sysadmin or a new Linux user, mastering environment variables is essential for customizing and controlling your shell experience.

In this guide, we'll take a dive into environment variables in the Linux shell. By the end, you'll not only know how to view and set these variables, but also how to persist them, use them in scripts, and troubleshoot issues effectively.

What Are Environment Variables?

At a basic level, environment variables are dynamic named values that affect the behavior of running processes on your Linux system. Think of them as configuration settings that your shell (like Bash or Zsh) and applications refer to in order to understand how they should operate.

For example:

  • The PATH variable tells the shell where to look for executable files.

  • The HOME variable stores the path to your home directory.

  • The LANG variable defines your system’s language and character encoding.

Environment Variables vs Shell Variables

There is an important distinction between shell variables and environment variables:

  • Shell variables are local to the shell session in which they are defined.

  • Environment variables are shell variables that have been exported, meaning they are inherited by child processes spawned from the shell.

Viewing Environment Variables

Before you can modify or use environment variables, it's important to know how to inspect them.

View All Environment Variables

printenv

or

env

Both commands list environment variables currently set for the session.

View a Specific Variable

echo $HOME

This will display the current user's home directory.

View All Shell Variables

set

This command displays all shell variables and functions. It's broader than printenv.

Setting and Exporting Environment Variables

You can define your own variables or temporarily change existing ones within your shell.

Setting a Temporary Variable

MY_VAR="hello" echo $MY_VAR

This creates a shell variable valid only for the current shell session.

Exporting a Variable

To make it an environment variable:

export MY_VAR="hello"

or

MY_VAR="hello" export MY_VAR

Using Variables

echo "My variable says: $MY_VAR"

Temporary PATH Update Example

export PATH=$PATH:/opt/myapp/bin

This appends /opt/myapp/bin to the existing PATH.

Making Environment Variables Persistent

Variables defined in the shell are lost once the session ends. To make them persistent, you need to store them in a shell configuration file.

Common Shell Startup Files
  • ~/.bashrc – Runs for interactive non-login shells.

  • ~/.bash_profile – Runs for login shells.

  • ~/.profile – General purpose login shell config (used by many desktop environments).

  • ~/.zshrc – For Zsh users, similar to .bashrc.

Example: Add to .bashrc

export EDITOR=nano export PATH="$HOME/.local/bin:$PATH"

Apply Changes Without Rebooting

source ~/.bashrc

or simply:

. ~/.bashrc

Commonly Used Environment Variables

Variable Description
PATH List of directories to search for executable files.
HOME User’s home directory.
USER Current logged-in username.
SHELL Path to the current shell binary.
PWD Current working directory.
EDITOR Preferred text editor (used by git, crontab, etc.).
LANG Language/locale setting.
PS1 Defines how the shell prompt appears.

Advanced Use Cases

Custom Shell Prompt

You can customize your shell prompt using the PS1 variable:

export PS1="\u@\h:\w\$ "

This shows: username@hostname:/current/directory$

Dynamic Variable Assignment

You can assign the output of a command to a variable:

NOW=$(date) echo "Current time: $NOW"

User-Defined Variables in Scripts

Shell scripts often use environment variables to control logic or paths:

#!/bin/bash GREETING="Hello" echo "$GREETING, $USER!"

Using Environment Variables in Shell Scripts

When writing scripts, understanding scope is key.

Local Scope

Variables by default are local to the script:

NAME="Linux"

Export to Subprocesses

To make them available to subprocesses:

export NAME="Linux"

Passing Variables to Scripts

You can pass environment variables to a script inline:

GREETING="Hello" ./myscript.sh

Using .env Files

Store environment variables in a file:

# .env file API_KEY=12345

Then load it:

source .env

Security and Debugging Tips

Avoid Exposing Sensitive Info

Be cautious when setting environment variables like passwords or tokens. They may be exposed through:

  • ps aux (when running commands inline).

  • Logs if improperly handled.

  • Accidental env output.

Prefer using secure vaults or files with restricted permissions.

Check if Variable Exists

if [ -z "$MY_VAR" ]; then echo "MY_VAR is not set" fi

Debugging Tips
  • Use set -x to enable debug mode in scripts.

  • Use declare -p VAR_NAME to inspect variable status.

  • env | grep VAR_NAME to check if it’s in the environment.

Conclusion

Environment variables are a fundamental part of the Linux shell. They provide essential configuration for your system and applications, make your scripts portable, and allow customization of your shell environment.

By learning to view, set, export, and persist these variables, you unlock a powerful set of tools for controlling your Linux experience. Whether you're optimizing your workflow, managing user profiles, or scripting complex systems, understanding environment variables will serve you well.

George Whittaker is the editor of Linux Journal, and also a regular contributor. George has been writing about technology for two decades, and has been a Linux user for over 15 years. In his free time he enjoys programming, reading, and gaming.

Load Disqus comments