Table of Contents
What environment variables are
Environment variables are named values stored by the shell and the operating system that programs can read. They influence how programs behave without changing the programs themselves.
Examples of common things controlled by environment variables:
- Where to search for commands (
PATH) - What your default editor is (
EDITOR,VISUAL) - What language/locale to use (
LANG) - Where your home directory is (
HOME) - What shell you are using (
SHELL)
They are always strings (text), even if they look like numbers.
Environment variables are different from:
- Shell variables: exist only inside a particular shell (and often not inherited by child processes).
- Configuration files: live on disk; environment variables live in memory while the shell/program runs.
Viewing environment variables
To see environment variables, you can use several commands. They show similar information, but with different scopes.
`printenv`
printenv is a simple, focused tool for environment variables.
Show all environment variables:
printenvShow a single variable:
printenv PATH
printenv HOME
If a variable is not set, printenv will print nothing and return an error code (useful in scripts).
`env`
env can show, temporarily modify, and run commands with a custom environment.
Show all environment variables:
envRun a command with a changed environment only for that command:
LANG=fr_FR.UTF-8 env date
After that command finishes, LANG in your shell is unchanged.
`set` (shell built-in, includes more than environment)
In most shells, set shows:
- shell variables
- environment variables
- shell functions
This output is usually long and not beginner-friendly, but it’s good to know:
set
For viewing just environment variables, prefer printenv or env.
Reading the value of a variable
Use $ followed by the variable name:
echo $HOME
echo $USER
echo $PATH
If a variable is not set, echo $SOMETHING prints an empty line.
When variable names are followed by other characters, use {} to make it clear:
echo "Home is ${HOME}dir" # safer than $HOMEdirImportant common environment variables
You will encounter these often:
PATH– colon-separated list of directories to search for commands.- Example:
/usr/local/bin:/usr/bin:/bin - When you type
ls, the shell looks forlsin each directory listed in$PATHin order. HOME– your home directory.- Example:
/home/alice - Used by many programs as a starting point for user-specific files.
SHELL– the full path to your login shell.- Example:
/bin/bash,/usr/bin/zsh USER/LOGNAME– your username.- Example:
alice LANG,LC_*– language/locale settings.- Example:
LANG=en_US.UTF-8 PWD– current working directory (the directory you’re “in” right now).- Used by the shell itself;
pwdcommand effectively prints$PWD. EDITOR/VISUAL– preferred text editor for command‑line tools.- Example:
EDITOR=nanoorEDITOR=vim - Used by tools like
crontab -e,git commit, etc.
These names are conventions; most are widely recognized by many programs.
Setting and changing environment variables (current shell)
There are two steps you need to distinguish:
- Setting a shell variable.
- Exporting it so that child processes (commands you run) can see it.
Temporary variable in the current shell
In Bash and similar shells:
MYVAR="hello"
echo $MYVAR
MYVAR exists only:
- in this shell session
- as a shell variable (not yet an environment variable)
Most simple programs will not see MYVAR until you export it.
Exporting a variable
export marks a variable so it is included in the environment of child processes.
Two common patterns:
- Set, then export:
MYVAR="hello"
export MYVAR- Set and export in one line:
export MYVAR="hello"
Now, commands you run can see MYVAR:
export MYVAR="hello"
env | grep MYVAR
To confirm a variable is exported, you can use export with no arguments:
exportThis lists exported variables (and functions).
Setting for a single command only
You can prefix a command with variable assignments. They apply only to that command:
LANG=de_DE.UTF-8 date
LANG is changed only for that date process. In your shell:
echo $LANGremains whatever it was before.
You can set multiple this way:
VAR1=one VAR2=two env | grep '^VAR'Unsetting environment variables
To remove a variable from the environment and shell:
unset MYVARAfter that:
echo $MYVAR # prints nothing
unset works for both shell and environment variables.
How environment variables are inherited
Key behavior:
- When you start a program from your shell, the new process inherits a copy of your environment variables.
- Child processes can change their own environment, but those changes do not go back up to the parent shell.
Example:
export CHILDVAR="visible to child"
bash # start a new shell as child
echo $CHILDVAR
Inside the child shell, $CHILDVAR is still available because it was inherited.
If you set a variable only in the child:
CHILDONLY="hi from child"
exit # go back to parent shell
echo $CHILDONLY
The parent shell does not see CHILDONLY.
This “one-way inheritance” is fundamental: environment flows from parent to child, not the other way.
Persisting environment variables across sessions
So far, variables disappear when you close the terminal. To keep them across logins, you add them to shell startup files. The exact files differ by shell and distribution, but for Bash you commonly use:
~/.bashrc– for interactive non-login shells (often terminals you open in the GUI).~/.profile,~/.bash_profile– for login shells (e.g., TTY login, some display manager setups).
A simple rule of thumb for beginners on typical desktop systems:
- Put most user-specific environment variables in
~/.bashrc.
Example:
- Edit
~/.bashrc:
nano ~/.bashrc- Add at the end:
export EDITOR="nano"
export PATH="$HOME/.local/bin:$PATH"- Apply the changes to your current shell:
source ~/.bashrc
# or
. ~/.bashrc
Now EDITOR and the modified PATH will be set every time a new Bash terminal starts.
System‑wide environment configuration (for all users) is usually done in files like /etc/environment or /etc/profile, but editing those requires sudo and is typically done by administrators.
Working with `PATH`
PATH is one of the most important variables. It controls where the shell looks for commands when you type a name without a path.
The format is:
dir1:dir2:dir3:...To see it:
echo $PATH
To add a directory to the front of PATH (higher priority):
export PATH="$HOME/bin:$PATH"To add to the end (lower priority):
export PATH="$PATH:$HOME/bin"
Common beginner mistake: overwriting instead of extending PATH. Avoid:
export PATH="$HOME/bin" # BAD: you lose all existing entries
Instead, always include the old $PATH unless you know exactly what you’re doing.
Environment variables and security
Environment variables can contain:
- Secrets (API keys, tokens, passwords)
- Paths to sensitive files
- Behavior flags that might weaken security
Basic safety guidelines:
- Do not blindly copy commands that export secrets into world‑readable files.
- Avoid putting long‑term secrets in
~/.bashrcor shared scripts; consider dedicated secret management tools where appropriate. - Remember that some commands (like
pson some systems) can show process environments, and logs might capture environment content in certain setups.
For everyday beginner use (like setting EDITOR, tweaking PATH), these concerns are minor, but be aware that environment variables are not inherently private or encrypted.
Practice ideas
Try these small exercises to get comfortable:
- Show all environment variables, then filter by a keyword:
printenv | grep HOME
printenv | grep SHELL- Create and export a variable, then confirm it’s visible to a child shell:
export MYCOLOR="blue"
bash
echo $MYCOLOR
exit- Temporarily run a command in another language (if locales are installed):
LANG=fr_FR.UTF-8 date
LANG=en_US.UTF-8 date- Safely add
~/binto yourPATHin~/.bashrc, reload it, and confirm:
echo $PATH
# edit ~/.bashrc to add:
# export PATH="$HOME/bin:$PATH"
source ~/.bashrc
echo $PATHThese small experiments will make environment variables feel much more intuitive.