Diagnose why an environment variable is missing in new shells, then make it persist across interactive sessions. Validate the change with repeatable CLI checks and evidence in shell startup files.
A developer reports that the PROJECT_ROOT
environment variable disappears in new terminals. You need to
reproduce the failure, identify which startup file is (or is
not) being read, and implement a persistent export that loads
in fresh interactive shells.
“It works in my current terminal” is not a fix. Treat this as complete only when a brand new shell session shows the variable and the export line is present in the correct file.
PROJECT_ROOT is set in the
current environment.
~/.bashrc.
~/.bashrc;
login shells may load ~/.profile or
~/.bash_profile depending on the system.
source applies changes immediately to the
current session, but does not prove persistence by itself.
grep) to confirm what
the shell will load.
PROJECT_ROOT is set.
printenv PROJECT_ROOT
# OR
env | grep PROJECT_ROOT
This establishes the baseline for the current shell. If the variable only exists in this session and disappears in a new terminal, the issue is persistence, not syntax.
/home/dev/project
# OR
PROJECT_ROOT=/home/dev/project
~/.bashrc for the export.
grep PROJECT_ROOT ~/.bashrc
~/.bashrc is a common location for user-level
exports intended to load in interactive shells. If there is
no match, the variable will not persist by default.
# No output means the variable is not defined in ~/.bashrc
echo 'export PROJECT_ROOT=/home/dev/project' >> ~/.bashrc
Appending an export line ensures that future interactive
shells that read ~/.bashrc set the variable
automatically.
source ~/.bashrc
This applies the export immediately to the current shell so you can verify the value without restarting the terminal.
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. The persistence check is opening a new terminal and repeating the same command.
grep PROJECT_ROOT ~/.bashrc
export PROJECT_ROOT=/home/dev/project
This is the durable proof that the fix is not accidental and will survive future sessions.
You proved the current environment, but you have not proved
persistence. Confirm the new shell is reading
~/.bashrc and that the export line is present and
not overridden later in the file.
Hard-coded paths drift. Validate that the target path exists and matches the project location. If the project location is per-user or per-host, move the value to a more appropriate profile mechanism.
If the variable is exported in multiple files, the last one read wins. Search your shell configs for additional exports and remove or consolidate to a single source of truth.
If the user runs a different shell, ~/.bashrc
will not apply. Confirm the actual shell and place the export
in the correct configuration file for that shell.
Remove the export line you added so the environment returns to its pre-lab state.
grep -n 'export PROJECT_ROOT=' ~/.bashrc
# Remove the matching line using your editor, then reload:
source ~/.bashrc
A fresh terminal no longer shows PROJECT_ROOT,
and grep finds no export line in
~/.bashrc.
printenv <var>
: Prints the value of an environment variable if it is set.
env
: Lists environment variables for the current process.
env | grep <pattern>
: Filters environment output for matching lines.
|
: Pipes output from the left command into the right
command.
grep <pattern>
: Prints lines matching the pattern.
grep <pattern> <file>
: Searches for matching lines in a file.
~/.bashrc
: Common startup file for interactive Bash shells.
echo 'export VAR=value' >> ~/.bashrc
: Appends an export line to the Bash startup file.
>>
: Appends output to the end of a file.
source <file>
: Loads a file into the current shell session.
source ~/.bashrc
: Applies changes without starting a new terminal.