Linux · Systems fluency
How do environment variables work on Linux?
Short answer
Environment variables are key-value pairs inherited by child processes. A shell variable becomes part of the environment only when exported, and each process receives a snapshot at launch.
The shell keeps its own variables and a separate exported environment. When it starts a child, the child receives a copy of the exported variables. Changing a variable later does not affect processes that are already running.
export APP_ENV=production
export PATH="$PATH:/opt/app/bin"
printenv APP_ENV
env | grep -i proxyWhere configuration belongs
- Login shells read profile files such as ~/.profile or ~/.bash_profile
- Interactive non-login shells often read ~/.bashrc
- systemd services should use Environment or EnvironmentFile in the unit
- Sudo may reset the environment depending on configuration
- Secrets belong in a secret manager, not a checked-in .env file
PATH order determines which binary wins when names collide. An attacker who can write to an early PATH directory can execute code under your account. Prefer absolute paths in privileged scripts and keep writable directories out of root PATH.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between a shell variable and an exported variable?
- 2Why does a service not see a variable set in your interactive shell?
- 3What are the risks of a writable directory early in PATH?
- 4Where should secrets be stored for a service?
Say it in the interview
Environment variables are inherited key-value pairs. A shell variable is local until exported, and children receive a snapshot at launch. Services do not inherit my interactive shell, so their environment must be defined in the unit. I keep secrets out of environment files where possible and avoid writable directories in PATH.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Linux mock interview and get scored feedback on depth, clarity and confidence.