what are environment variables in linux
Environment variables in Linux are dynamic key-value pairs that configure the runtime environment for processes, shells, and applications. They store settings like paths, user info, and system behaviors, inherited by child processes for consistent operation.
Core Definition
These variables act like a shared configuration map, telling programs where to find files, what language to use, or how to behave. For instance, PATH lists directories for executable searches, preventing you from typing full paths every time. Unlike shell variables (local to one session), environment variables get exported to persist across subshells.
From a sysadmin's view: They're essential for scripting and apps. Developers love them for hiding secrets like API keys without hardcoding.
Types of Variables
Linux distinguishes these categories:
Type| Scope| Persistence| Example Files| Notes 139
---|---|---|---|---
System-wide| All users/processes| Across reboots| /etc/environment,
/etc/profile| Needs root; global impact.
User-specific| Single user| Across logins| ~/.bashrc,
~/.bash_profile| Loaded per shell startup.
Session-only| Current shell| Temporary| None (set via export)| Vanishes
on exit.
Shell variables| Current shell only| None| Internal to bash/zsh| Not
inherited.
System-wide ones load first via PAM modules, overriding user tweaks if conflicting.
Common Examples
Here's a rundown of everyday variables you'll encounter:
- PATH : Colon-separated dirs for commands (e.g.,
/usr/bin:/home/bin).
- HOME : Your user's home folder (e.g.,
/home/username).
- USER/LOGNAME : Current username.
- SHELL : Path to your shell (e.g.,
/bin/bash).
- *LANG/LC_ **: Locale settings for language, dates, numbers (e.g.,
en_US.UTF-8).
- TERM : Terminal type (e.g.,
xterm-256color).
Pro Tip : Run
env | grep PATHto inspect yours—it's often cluttered with duplicates over time.
Listing and Inspecting
Use these commands for visibility:
envorprintenv: All exported environment variables.printenv NAME: Single variable (e.g.,printenv PATH).set: Everything, including non-exported shell vars (verbose!).
Newer distros like Ubuntu 25.04 emphasize env for clean output in scripts.
Setting Variables
Temporary (session-only) :
export MYVAR="hello"
Child processes inherit it; current shell too.
Persistent :
- User: Add
export MYVAR="value"to~/.bashrc, thensource ~/.bashrc. - System: Edit
/etc/environmentas root (noexportneeded).
Story Time : Imagine debugging a Node.js app failing on a server—turns out
NODE_ENV=production wasn't set in /etc/environment. One export later, it
hummed perfectly, saving hours. Real-world wins like this make env vars a
Linux superpower.
Multiple Perspectives
- Dev Angle : Secure config injection (e.g., Docker uses them heavily).
- Ops View : Avoid hardcoding in crons; use for DB creds safely.
- Power User : Customize with
export EDITOR=vimfor seamless tools.
Forum chatter on Reddit/Linux subs (as of early 2026) raves about aliasing
export in .zshrc for speed, but warns against bloating PATH.
Quick Management Tips
- Unset:
unset VAR. - Append PATH:
export PATH=$PATH:/new/dir. - Check inheritance: Spawn
bash, runenv—see what's passed down.
TL;DR : Environment variables power Linux flexibility—master them via
export, files like .bashrc, and commands like env. They're global config
glue, evolving with distros like Fedora 44's PAM tweaks.
Information gathered from public forums or data available on the internet and portrayed here.