Loading...

Lab 33: Environment Variables and Shell Configuration

Diagnose why an environment variable is missing in new shells, then make it persist correctly across terminal sessions. Validate the change using repeatable CLI checks and evidence in shell config files.

core troubleshooting services

Scenario

A developer reports that the PROJECT_ROOT environment variable does not persist across terminal sessions. Your task is to confirm the issue, determine where it should be defined, and ensure it loads automatically in all new sessions.

Operator context

“Works in my current terminal” is not good enough. You need persistence across fresh shells and evidence that the export line is present in the right startup file.

Objective

  • Check whether PROJECT_ROOT is currently set.
  • Confirm it is missing from ~/.bashrc.
  • Add a correct export line to persist the variable.
  • Reload shell configuration and verify the environment.
  • Confirm the line exists in ~/.bashrc as evidence.

What You’ll Practice

  • Inspecting environment variables using printenv and env.
  • Auditing shell initialization files with grep.
  • Persisting exports in ~/.bashrc safely.
  • Applying changes immediately with source.
  • Proving persistence with repeatable verification commands.

Walkthrough

Step 1 : Check if PROJECT_ROOT is set in the current session.
Command
printenv PROJECT_ROOT
# OR
env | grep PROJECT_ROOT

This confirms whether the variable exists in the current environment. If it disappears after opening a new terminal, it is almost always a shell init file issue, not a “Linux is broken” issue.

/home/dev/project
# OR
PROJECT_ROOT=/home/dev/project
Step 2 : Check if PROJECT_ROOT is defined in ~/.bashrc.
Command
grep PROJECT_ROOT ~/.bashrc

~/.bashrc is a common place for user-level exports that must load in interactive shells. If the export line is missing, the variable will not persist.

# No output means the variable is not set in ~/.bashrc
Step 3 : Add the export line to persist the variable.
Command
echo 'export PROJECT_ROOT=/home/dev/project' >> ~/.bashrc

Appending the export line makes it load automatically for future interactive shells that read ~/.bashrc.

Step 4 : Reload the shell configuration immediately.
Command
source ~/.bashrc

Reloading applies the change to the current terminal without needing to close and reopen the session.

Step 5 : Verify the variable is now present.
Command
printenv PROJECT_ROOT
# OR
env | grep PROJECT_ROOT
/home/dev/project
# OR
PROJECT_ROOT=/home/dev/project

This confirms the export is live in the current environment. For true persistence, open a new terminal and re-run the same check.

Step 6 : Confirm the export line exists in ~/.bashrc.
Command
grep PROJECT_ROOT ~/.bashrc
export PROJECT_ROOT=/home/dev/project

This is your evidence that the fix is persistent and repeatable, not a one-time environment accident.

Reference

  • printenv VAR : Prints the value of an environment variable if it is set.
  • env : Lists environment variables for the current process.
  • grep PATTERN FILE : Searches for matching lines (used here to audit startup files).
  • ~/.bashrc : Common startup file for interactive Bash shells.
  • source FILE : Loads a file into the current shell session without restarting.