Configuring Environment Variables in Linux

Setting Environment Variables in Linux

Linux offers two approaches for configuring environment variables: temporary and permanent.

Temporary Configuration

Variables set this way disappear when the terminal closes. Use the export command directly:

export APP_VAR="Hello World"
export PATH=$PATH:/opt/myapp/bin

Alternatively, assign first then export:

CUSTOM_VAR="example"
export CUSTOM_VAR

Permanent Configuration

To make variables persistent, modify shell configuration files. These split into user-level and system-level configurations. The loading sequence depends on your shell type (Bash, Zsh, Fish), login method, and distribution.

Shell types include:

  • Login shells: SSH connections, local terminal login, su - or su -l commands
  • Non-login shells: GUI terminal windows, su username, script execution

Configuration File Hierarchy

System-wide files (all users):

  • /etc/profile — Loaded first during system login
  • /etc/bash.bashrc or /etc/bashrc — Loaded when opening any terminal
  • /etc/environment — Loaded at boot, accessible to all processes

User-specific files:

  • ~/.profile
  • ~/.bash_profile
  • ~/.bashrc — Most frequently used

Shell Loading Order

Login shells initialize by reading files sequentially:

  1. /etc/profile
  2. ~/.bash_profile
  3. ~/.profile
  4. ~/.bashrc

Non-login shells inherit parent shell variables and load:

  1. /etc/bashrc
  2. ~/.bashrc

System-wide environment (/etc/environment):

  • Loads at system startup
  • Independent of login/non-login status
  • Accessible to every process and user

Determining shell type: Check the $0 variable:

echo $0

Output -bash indicates a login shell; bash indicates non-login.

Modifying Configuration Files

User-level configuration:

vim ~/.bashrc
# Append these lines:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:/home/user/tools/bin
# Reload using either method:
source ~/.bashrc
# or restart the terminal

System-level configuraton:

For /etc/environment (requires sudo, no export keyword):

sudo vim /etc/environment
# Format example:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
JAVA_HOME="/usr/lib/jvm/java-17-openjdk"
# Reboot or re-login to apply changes

For /etc/profile (affects all users):

sudo vim /etc/profile
# Add configuration:
export PATH=$PATH:/opt/custom-tools/bin
export LANG=en_US.UTF-8
# Apply immediately:
source /etc/profile

Tags: Linux Environment Variables Shell Configuration System Administration bash

Posted on Thu, 11 Jun 2026 18:27:02 +0000 by blacksmoke26