Linux Essentials Preparation · lesson 13 of 23 · 25 min
Environment Variables
Environment variables are named values stored in the shell environment. They configure the behavior of the shell and programs. Inherited by child processes.
Key environment variables:
PATH — colon-separated list of directories searched for commands. echo $PATH → /usr/local/bin:/usr/bin:/bin. When you type ls, the shell searches PATH directories for the ls binary.
HOME — current user’s home directory. echo $HOME → /home/alice.
USER — current username. echo $USER → alice.
SHELL — path to the current shell. echo $SHELL → /bin/bash.
PWD — current working directory (same as pwd command).
EDITOR / VISUAL — default text editor. Used by programs that open an editor (git commit, crontab -e).
LANG / LC_ALL — locale settings (language, character encoding).
Managing Environment Variables
env — displays all current environment variables. env | grep PATH filters for PATH.
set — displays all shell variables (including local variables not exported to environment). More output than env.
export VAR=value — creates or updates an environment variable and exports it to child processes. export EDITOR=vim.
unset VAR — removes a variable. unset EDITOR.
Local vs exported variables: name="Alice" creates a local shell variable — not inherited by child processes. export name="Alice" makes it an environment variable — inherited.
Modifying PATH: export PATH="$PATH:/opt/myapp/bin" — appends a directory to PATH. Changes are lost when the shell exits unless added to ~/.bashrc or ~/.bash_profile.
Persistent variables: Add export statements to ~/.bashrc (interactive shells) or ~/.bash_profile (login shells). source ~/.bashrc reloads without logging out.
Exam Tip
export makes a variable available to child processes. unset removes it. PATH determines where the shell looks for commands. Add to ~/.bashrc for persistent changes. source reloads config files.
Key Terms
Key Terms
Knowledge Check — 8 Questions
Linux Essentials — Environment Variables
The PATH variable determines:
- The current directory
- Where the shell looks for command binaries
- The user’s home directory
- The default shell
- Answer: Where the shell looks for command binaries
When you type a command, the shell searches each directory in PATH in order. If not found, you get “command not found”.
export EDITOR=vim does what?
- Opens vim
- Sets EDITOR as a local variable only
- Sets EDITOR and makes it available to child processes
- Installs vim
Answer: Sets EDITOR and makes it available to child processes
export makes the variable an environment variable — inherited by any child processes (programs) started from this shell.
To display all environment variables, use:
- set
- env
- printenv
- Both env and printenv
- Answer: Both env and printenv
env and printenv both display environment variables. set shows more (including local shell variables). All three are valid. To add /opt/myapp/bin to PATH permanently, you should:
- Run export PATH=$PATH:/opt/myapp/bin in the terminal
- Add export PATH=”$PATH:/opt/myapp/bin” to ~/.bashrc
- Edit /etc/PATH
- Use the path command
- Answer: Add export PATH=”$PATH:/opt/myapp/bin” to ~/.bashrc
Terminal changes are lost when the shell exits. Add to ~/.bashrc for persistence — it runs every time a new interactive shell starts. source ~/.bashrc:
- Deletes .bashrc
- Reloads .bashrc in the current shell without logging out
- Creates a new shell
- Backs up .bashrc
- Answer: Reloads .bashrc in the current shell without logging out
source (or .) executes the file in the current shell — changes take effect immediately without needing to log out and back in. unset EDITOR removes:
- The vim editor
- The EDITOR environment variable
- All environment variables
- The .bashrc file
- Answer: The EDITOR environment variable
unset removes a variable from the shell environment. After unset EDITOR, echo $EDITOR returns nothing. $HOME contains:
- The root directory
- The current user’s home directory
- The current directory
- The shell path
- Answer: The current user’s home directory
$HOME = /home/username (or /root for root). It is the same as ~ in path expansion.
The _____ variable contains the path to the current user’s default shell.
Answer: SHELL
$SHELL = /bin/bash (or /bin/zsh, /bin/sh, etc.) — the shell that was started when the user logged in.
Previous lesson: Regular expressions: basic and extended regex, grep -E, sed, awk patterns
Back to Linux Essentials Preparation
Next lesson: Linux file system hierarchy: /, /etc, /var, /home, /tmp, /proc, /dev, /usr, /bin; file types