Linux

Environment Variables in Linux: Complete Setup Guide

This guide explains what environment variables are in Linux, how to view them, set them temporarily, and configure them permanently. You'll learn to manage paths, application variables, and your development environment.

Updated at February 17, 2026
10-15 min
Easy
FixPedia Team
Применимо к:Ubuntu 20.04+Debian 11+CentOS/RHEL 8+Bash 4.0+Zsh 5.0+

Introduction / Why This Matters

Environment variables are a key mechanism in Linux for passing information between processes and configuring program behavior. They control executable search paths (PATH), localization (LANG), specific application settings (like JAVA_HOME), and much more. Understanding how to configure them properly is a fundamental skill for developers, system administrators, and any advanced Linux user. This guide will help you work confidently with environment variables—from simple viewing to creating persistent global settings.

Prerequisites / Preparation

  • Operating System: Any Linux distribution (Ubuntu, Debian, CentOS, Fedora, Arch, etc.).
  • Command Shell (Shell): The guide focuses on bash (default in most distributions) and zsh. The export syntax is standard.
  • Access Permissions: Standard user permissions are sufficient for editing files in your home directory (~/.bashrc, ~/.profile).
  • Text Editor: Familiarity with nano (simple) or vim/vim.tiny (powerful) is recommended.

Step 1: Viewing Current Environment Variables

Before changing anything, you need to understand what's already there. Several commands are available for this:

  • env — prints all environment variables of the current session.
  • printenv — works similarly to env, but can accept a variable name: printenv PATH.
  • echo $VAR_NAME — shows the value of a specific variable in the current shell. Convenient, but the variable must already be defined in the shell.
# Show all variables
env

# Show only PATH
echo $PATH

# Check if a variable exists
printenv HOME

Pay attention to the PATH variable. This is a list of directories through which the system searches for executable commands. Directories are separated by a colon (:).

Step 2: Creating a Temporary Variable (For the Current Session)

To set a variable that will be available only in the current open terminal and its child processes, perform two actions:

  1. Assign a value: MY_VAR="value".
  2. Export to the environment: export MY_VAR.
# Create a variable and export it
export FAVORITE_EDITOR="nano"
export PROJECT_PATH="/home/user/myproject"

# Verify it's accessible
echo $FAVORITE_EDITOR
# Output: nano

# Launch a child process (e.g., another script) that will see this variable
./some_script.sh

⚠️ Important: If you simply run MY_VAR="value" without export, the variable will exist only in the current shell but will not be passed to programs launched from it.

Step 3: Modifying the PATH Variable

One of the most common tasks is adding your own directory with executable files to PATH. Always append your new path to the existing $PATH value, rather than overwriting it, or you will break access to system commands (ls, cd, apt, etc.).

# Correct: add the ~/bin directory to the end of PATH
export PATH="$PATH:$HOME/bin"

# Check the result
echo $PATH
# The output will end with :/home/your_username/bin

# You can add to the beginning (higher priority), but be cautious
# export PATH="$HOME/bin:$PATH"

Step 4: Creating Persistent Variables via ~/.bashrc

The ~/.bashrc file (the leading dot makes it hidden) is executed every time a new interactive non-login shell starts. This is the ideal place for variables you need in every new terminal.

  1. Open the file in a text editor:
    nano ~/.bashrc
    
  2. Add export command lines for your variables at the end of the file:
    # My custom settings
    export EDITOR="nano"
    export LANG="ru_RU.UTF-8"
    export PATH="$PATH:$HOME/.local/bin"
    
  3. Save the file (Ctrl+O in nano, then Enter) and close the editor (Ctrl+X).
  4. Apply the changes to the current session without restarting the terminal:
    source ~/.bashrc
    
    Or simply open a new terminal window.

Step 5: Creating Global Variables via ~/.profile (or ~/.bash_profile)

If you need a variable to be available upon any system login (including graphical login, scripts run via cron, or other programs launched from menus), use the ~/.profile file (or ~/.bash_profile on some systems). This file is read when a login shell starts.

  1. Open (or create) the file:
    nano ~/.profile
    
  2. Add the necessary export commands there. Example for JAVA_HOME:
    # Java configuration
    export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
    export PATH="$JAVA_HOME/bin:$PATH"
    
  3. Save the file. Changes will take effect after the next login (reboot or log out/in). To apply them now in the current terminal, run:
    source ~/.profile
    

💡 Tip: For most users who primarily work in the terminal, ~/.bashrc is sufficient. Use ~/.profile for settings that must be visible to all programs running under your username.

Step 6: Creating a Variable for a Single Command (Inline)

Sometimes you need to set a variable only to run one specific command, without polluting the environment. Syntax: VAR=value command.

# Run 'make' with a specific compiler specified
CC=gcc-11 make

# Launch a script with a temporary locale
LANG=ru_RU.UTF-8 ./setup_script.sh

# Verify the global variable remains unchanged
echo $LANG
# Output will be the same as before if set in .bashrc/.profile

Verifying the Result

After applying your settings, ensure everything works:

  1. Restart the terminal (or run source for the relevant file).
  2. Check for your variables:
    echo $MY_VAR
    printenv MY_VAR
    
  3. Check that PATH contains the needed directories in the correct order:
    echo $PATH | tr ':' '\n'  # Output each path on a new line
    
  4. Verify that commands from the added directory are accessible:
    which my_custom_tool  # Should show the full path to the executable file
    

Common Issues

  • "Command not found" after adding to PATH: Ensure you added the path to a directory, not to the executable file itself. Verify the file exists and has execute permissions (chmod +x file).
  • Changes in .bashrc/.profile not applying: Are you sure you edited the correct file? Check which shell you use (echo $SHELL). Ensure you ran source ~/.bashrc or restarted the terminal. For .profile, a full logout/login may be required.
  • Variable name conflicts: Avoid using common names (PATH, HOME, USER) for your own variables unless you understand the consequences. Use a prefix, e.g., MYAPP_DEBUG.
  • Permission issues: When running scripts or programs that depend on your variables, ensure you have execute permissions. Access errors can occur if the script is run via sudo—by default, your current user's variables are not passed. Use sudo -E to preserve the environment (use cautiously!) or configure variables system-wide in /etc/environment (requires root privileges).
  • Encoding/Locale: If characters display incorrectly after setting LANG or LC_* variables, ensure the corresponding locale generation is installed on your system (sudo locale-gen ru_RU.UTF-8 on Debian/Ubuntu).

F.A.Q.

What's the difference between .bashrc and .profile?
Why aren't my variables persisting after reboot?
How do I add a directory to the PATH variable?
Can I set an environment variable for a single command?

Hints

View current environment variables
Create a temporary variable
Add a directory to PATH
Create a permanent variable (for Bash)
Create a permanent variable (for all sessions)
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community