Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Check and Set Environment Variables in Linux

Published

Linux

Environment variables are an essential part of how Linux—and really, any Unix-like system—works behind the scenes. They quietly hold information that your system, applications, and shell depend on every day. Whether you’re launching a program, logging in, or writing a script, environment variables shape what happens next.

Knowing how to check and set these variables will help you troubleshoot problems, customize your workflow, and automate tasks more effectively. In this guide, you’ll learn what environment variables are, why they matter, and the practical steps to view, set, and manage them on any Linux system.

Table of Contents

  1. What Are Environment Variables?
  2. Why Are Environment Variables Important?
  3. How to View Environment Variables
    1. Viewing All Variables
    2. Viewing a Specific Variable
    3. Viewing Variables for a Single Command
  4. How to Set Environment Variables
    1. Setting Variables Temporarily
    2. Setting Variables for a Single Command
    3. Setting Variables Permanently
  5. Where to Set Environment Variables for Permanence
  6. Practical Examples of Environment Variables
  7. Troubleshooting and Tips
  8. Frequently Asked Questions (FAQ)
  9. Conclusion

1. What Are Environment Variables?

Environment variables are simple key-value pairs stored by your shell (like Bash or Zsh) and used by Linux and its applications. Think of them as little notes that tell programs how to behave, where to look for files, what your username is, what language to use, where temporary files live, and much more.

For example, the variable HOME tells programs where your home directory is, PATH lists the directories to search for commands, and LANG defines your language and locale.

2. Why Are Environment Variables Important?

  • System behavior: They control how applications start and interact with the environment.
  • Customization: You can tweak how your shell, editors, and tools work—without changing the programs themselves.
  • Scripting: Scripts often use environment variables to adapt to different users or systems.
  • Troubleshooting: Many issues (like commands not being found or programs acting weird) come down to environment variables being set (or not set) correctly.

3. How to View Environment Variables

Viewing All Variables

If you want to see everything that’s set in your current shell, use:

printenv

Or, for a more detailed list (including shell variables):

env

Or, to see both shell and environment variables:

set

You’ll see output like:

HOME=/home/username
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG=en_US.UTF-8

Viewing a Specific Variable

To check the value of a specific variable, use echo:

echo $HOME
echo $PATH
echo $SHELL

The $ tells the shell you want the value, not the variable name.

Viewing Variables for a Single Command

Sometimes you want to see what variables a program sees. You can run, for example:

env | grep PATH

Or launch a program with a new variable:

FOO=bar env

4. How to Set Environment Variables

Setting Variables Temporarily

To set a variable for your current shell session only:

export MYVAR=value

For example:

export EDITOR=vim

After this, any program or script you start in that shell will see EDITOR as vim. Close the terminal, and it’s gone.

Setting Variables for a Single Command

You can define a variable just for one command:

MYVAR=value somecommand

Example:

LANG=fr_FR.UTF-8 man ls

This runs man ls as if your language was set to French, but doesn’t affect the rest of your session.

Setting Variables Permanently

If you want a variable to be set every time you log in or open a shell, you need to put it in one of your shell’s config files.

5. Where to Set Environment Variables for Permanence

  • ~/.bashrc: Runs for each new interactive shell. Good for variables used in terminals.
  • ~/.profile or ~/.bash_profile: Ran at login; best for variables other programs or GUIs need.
  • ~/.bash_aliases: Often sourced by .bashrc if present (good for organization).

Example: To add a variable permanently:

    • Open your .bashrc or .profile:
nano ~/.bashrc
    • Add your variable at the end:
export MYNAME="QuillBotUser"
    • Save and close the file.
    • Reload your configuration:
source ~/.bashrc

Now, MYNAME will be available every time you open a terminal.

Per-user vs. System-wide

For just your user: Edit files in your home directory (~/.bashrc, ~/.profile).

For all users: Edit /etc/environment or /etc/profile (requires sudo).

6. Practical Examples of Environment Variables

  • PATH: Determines where your shell looks for commands.
    export PATH=$PATH:/opt/myapp/bin
  • EDITOR: Sets your preferred command-line editor.
    export EDITOR=nano
  • LANG: Sets your language and character encoding.
    export LANG=en_US.UTF-8
  • PYTHONPATH: Tells Python where to look for modules.
    export PYTHONPATH=$HOME/projects/mylib
  • JAVA_HOME: Points to your Java install for Java-based programs.
    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

7. Troubleshooting and Tips

  • Variable not sticking? Make sure you’re editing the correct file and using export.
  • Changes not showing up? Run source ~/.bashrc or open a new terminal.
  • PATH errors? Don’t overwrite PATH—always append/prepend using $PATH (e.g., export PATH=/new/path:$PATH).
  • Check for typos: Misspellings can break scripts and commands.
  • Order matters: If two config files set the same variable, the last one loaded wins.
  • System variables: Don’t remove or overwrite variables you don’t recognize unless you know what they do!

8. Frequently Asked Questions (FAQ)

Q: What’s the difference between shell and environment variables?
A: Shell variables are only available in your current shell. Environment variables are exported (using export) and inherited by child processes and programs.

Q: How do I see which variables are exported?
A: Run export in the shell.

Q: Can I remove an environment variable?
A: Yes, use unset VARNAME (e.g., unset EDITOR) to remove it for your current shell session.

Q: How do I check environment variables for a running process?
A: Use cat /proc/PID/environ (replace PID with the process ID), or ps eww -p PID.

Q: Why isn’t my variable available in scripts or GUIs?
A: Make sure you set it in a file that applies to login shells and graphical sessions, like .profile or /etc/environment.

9. Conclusion

Environment variables are one of those “invisible” parts of Linux that quietly shape your computing experience. Once you know how to check, set, and manage them, you unlock a new level of customization and power—whether you’re scripting, troubleshooting, or just trying to set your favorite editor once and for all.

Start by exploring what’s already set on your system. Practice changing variables for just one terminal, for a single command, and then make a permanent addition to your .bashrc or .profile. With a few tweaks, you’ll find Linux becomes even more adaptable, personal, and efficient.

Related guides

Account services are unavailable until the production backend is configured.