How to Configure User Environment Variables in Linux

How to Configure User Environment Variables in Linux

Hey there, and welcome to this beginner-friendly guide on configuring user environment variables in Linux. If you've ever wondered how to set your PATH or define a custom variable that persists when you open a new terminal — you're in the right place.

Let’s take this step by step, in a way that’s clear, practical, and hands-on.

🔍 What Are Environment Variables?

Environment variables are like global values that live inside your terminal session. They can store paths, settings, and preferences your shell and apps can use.

For example, the PATH variable tells Linux where to look for executable programs.

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Let’s say you want to add a new folder to your PATH or create your own variable like MY_NAME. That’s what we’ll learn next!

🛠️ Step 1: Create a Temporary Variable

This variable works only for the current terminal session:

export MY_NAME="Alex"
echo $MY_NAME
Alex

But if you close this terminal, it’s gone. To make it permanent, you need to add it to a shell configuration file.

📄 Step 2: Add Variables to .bashrc or .zshrc

If you're using the Bash shell (most common), open your ~/.bashrc file:

nano ~/.bashrc

Scroll to the bottom and add:

export MY_NAME="Alex"
export PATH="$PATH:/home/alex/my-tools/bin"

Save and exit (in nano, press Ctrl + X, then Y, then Enter).

✅ Step 3: Apply the Changes

Now run:

source ~/.bashrc

This reloads your terminal configuration and applies the changes.

echo $MY_NAME
Alex

👀 Step 4: Set Variables for All Users (Optional)

To set environment variables system-wide, edit /etc/environment (requires sudo):

sudo nano /etc/environment

Add variables like this (no export needed):

MY_NAME=Alex
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Save and restart your session for changes to take effect.

🧠 Summary

  • Use export VAR=value to define a variable temporarily.
  • Add export lines to ~/.bashrc or ~/.zshrc for permanent use.
  • Use /etc/environment for system-wide settings.

And that’s it! You’ve just learned how to configure environment variables like a pro. These tricks come in super handy when setting up development tools, custom scripts, or just personalizing your terminal experience.

See you in the next tutorial!