Interview Q&A / Linux & Shell

BeginnerIn-depth3 min readUpdated 2 Oct 2026

What are environment variables in Linux, and what does the PATH variable do?

SHORT ANSWER

Environment variables are named values (like HOME, USER or PATH) that every process inherits from its parent. PATH is a colon-separated list of directories the shell searches, in order, to find a command you type, so you can run python3 instead of /usr/bin/python3.

Explain it simply

When a new employee joins an office, they are handed a small card with basic details: their desk number, the Wi-Fi name, which floor the canteen is on. Every program in Linux gets a similar card when it starts. The entries on that card are environment variables: simple NAME=value pairs such as HOME=/home/ravi or LANG=en_IN.UTF-8.

PATH is one entry on that card, and it is a list of folders. When you type git, the shell does not search the whole disk. It checks each folder in PATH, in order, and runs the first git it finds.

Example

bash
echo $HOME
echo $PATH
text
/home/ravi
/usr/local/bin:/usr/bin:/bin:/home/ravi/.local/bin

See which file a command actually runs:

bash
which python3
type ls
text
/usr/bin/python3
ls is aliased to `ls --color=auto'

Setting variables:

bash
GREETING="namaste"          # shell variable, only this shell sees it
export API_URL="https://api.example.com"   # environment variable, child processes see it
printenv API_URL
env | grep API

DEBUG=1 node app.js         # set a variable for one command only

Adding your own folder to PATH:

bash
export PATH="$HOME/bin:$PATH"

To make that permanent, add the line to ~/.bashrc (or ~/.zshrc for zsh) and run source ~/.bashrc.

How it works

Each process has its own copy of its environment. When a process starts a child (for example, your shell starting node), the child receives a copy of the parent's exported variables. Changes in the child never flow back to the parent. That is why running a script that does export X=1 does not change X in your terminal; you need source script.sh, which runs it inside the current shell.

A plain assignment like GREETING=namaste creates a shell variable that is not passed to children. export marks it for the environment.

For PATH, order matters. If both /usr/local/bin/python3 and /usr/bin/python3 exist, the one in the folder listed first wins. Tools like nvm and pyenv work by putting their own folder at the front of PATH.

Real-world use

  • Keeping secrets and settings out of code: DATABASE_URL, PORT, NODE_ENV=production, often loaded from a .env file in development.
  • Docker and Kubernetes pass configuration to containers as environment variables.
  • JAVA_HOME, GOPATH and similar variables tell tools where things are installed.
  • Fixing "command not found" after installing a tool, by adding its bin folder to PATH.

Common mistakes

  • Writing PATH=$HOME/bin without :$PATH. This replaces the whole PATH and suddenly ls and cat stop working in that shell.
  • Spaces around =: export X = 1 is an error; it must be export X=1.
  • Expecting ./script.sh that exports variables to change your current shell.
  • Putting . (the current folder) in PATH, which can make you run a malicious file named ls by accident.
  • Printing secrets from environment variables into logs.

What to say in the interview

"Environment variables are key-value pairs that a process inherits from its parent, like HOME, USER, LANG and PATH. I set them with export, and they flow down to child processes but never back up. PATH is a colon-separated list of directories that the shell searches in order to find an executable, so the first match wins. To add a tool permanently I append export PATH="$HOME/bin:$PATH" to my .bashrc. In applications I use environment variables for configuration and secrets so they stay out of the code."

Follow-up questions

  • What is the difference between a shell variable and an environment variable?
  • What is the difference between .bashrc and .bash_profile?
  • Why does source script.sh behave differently from ./script.sh?
  • Why does a command work in your terminal but not in a cron job?

#linux#shell#environment#path

Related questions

Practise more Linux & Shell questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Linux & Shell quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.