How do environment variables work in Linux and what is the PATH?
Understand how Linux environment variables work, the difference between set and export, and how the PATH variable tells the shell where to find commands.
Expected Interview Answer
Environment variables are named key-value pairs stored in a process's environment that configure its behaviour and are inherited by child processes; PATH is the environment variable listing the colon-separated directories the shell searches to locate executable commands.
When you run a command like ls, the shell walks each directory in PATH left to right and runs the first matching executable it finds. Variables are set with NAME=value, but only become environment variables (visible to child processes) when exported with export. Persistent variables are defined in startup files such as ~/.bashrc, ~/.profile or /etc/environment, whereas a variable set without export stays a shell-local variable only.
- Lets you run commands by name without typing full paths
- Configures programs without changing their code
- Values are inherited by child processes automatically
- Enables per-user and system-wide customisation
- Supports switching tools and versions via PATH ordering
AI Mentor Explanation
PATH is like the ordered list of grounds the team bus checks to find today's net session: it drives to the first venue on the list, and if the nets aren't there it moves to the next. Environment variables are the standing team instructions — kit colour, warm-up time — passed down to every junior squad so each player inherits the same brief without being told individually.
Step-by-Step Explanation
Step 1
Inspect the environment
Run env or printenv to list all current environment variables inherited by your shell.
Step 2
View PATH
Run echo $PATH to see the colon-separated directories the shell searches for executables, in order.
Step 3
Set a shell variable
Use NAME=value to create a variable local to the current shell, not yet visible to child processes.
Step 4
Export it
Run export NAME to promote it to an environment variable so child processes inherit it.
Step 5
Extend PATH
Prepend a directory with export PATH="$HOME/bin:$PATH" so your own binaries are found first.
Step 6
Make it persistent
Add the export line to ~/.bashrc or ~/.profile so it applies to future login shells.
What Interviewer Expects
- Defines environment variables as inherited key-value pairs
- Explains PATH as an ordered search list for executables
- Knows the difference between set and export
- Can name persistence files like ~/.bashrc and /etc/environment
- Understands left-to-right resolution order in PATH
Common Mistakes
- Setting a variable without export and expecting children to see it
- Overwriting PATH instead of appending, breaking core commands
- Confusing shell variables with environment variables
- Using a comma or space instead of a colon as the separator
- Editing the wrong startup file for the shell in use
Best Answer (HR Friendly)
“Environment variables are small named settings that programs read to know how to behave, and they pass down to any programs they start. PATH is one such setting that tells the system which folders to look in when you type a command, so you can run tools by name instead of typing their full location.”
Code Example
# Shell-local variable (children can't see it)
GREETING=hello
# Promote it to the environment
export GREETING
# View and extend PATH (prepend so your bin wins)
echo $PATH
export PATH="$HOME/bin:$PATH"
# Find which executable the shell will run
which python3
# Persist for future shells
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrcFollow-up Questions
- What is the difference between a shell variable and an environment variable?
- How do you make an environment variable persist across reboots?
- What happens if you accidentally overwrite PATH?
- How does the shell resolve a command name using PATH?
- What is the difference between ~/.bashrc and ~/.profile?
MCQ Practice
1. What does the PATH environment variable contain?
PATH is a colon-separated list of directories the shell searches, in order, to locate the executable for a typed command.
2. Which command promotes a shell variable so child processes inherit it?
export marks a variable as part of the environment, making it visible to child processes; a plain assignment stays shell-local.
3. How are directories separated in the PATH variable on Linux?
Linux uses a colon (:) to separate PATH directories, unlike Windows which uses a semicolon.
Flash Cards
What is an environment variable? — A named key-value pair in a process's environment that configures behaviour and is inherited by child processes.
What is PATH? — An environment variable listing colon-separated directories the shell searches, in order, to find executables.
set vs export? — A plain assignment creates a shell-local variable; export promotes it to an environment variable seen by child processes.
How to persist a variable? — Add the export line to a startup file like ~/.bashrc, ~/.profile, or /etc/environment for system-wide scope.
Continue Learning
Related Interview Questions
What is the difference between the > and >> redirection operators in Linux?
easy
What is the difference between absolute and relative paths in Linux?
easy
What is the difference between a shell and a terminal in Linux?
easy
What is the difference between a login shell and a non-login shell in Linux?
medium