Loading...

Lab 55: Environment Variables

Inspect and modify environment variables to troubleshoot session state issues that produce inconsistent tool behavior. Validate PATH, set and verify a project variable, persist it for new shells, then remove it cleanly.

troubleshooting core users

Scenario

A user reports “command not found” behavior and inconsistent application paths between sessions. Your job is to inspect the current environment, validate PATH, set a project directory variable for a deployment workflow, persist it, then remove it safely.

Operator context

Environment variables influence process behavior, command lookup, and tool defaults. The operational workflow is to validate current state, apply a minimal change, verify it immediately, then persist it only when required.

Objective

  • Print the current PATH value.
  • List environment variables in the current session.
  • Create and export PROJECT_DIR.
  • Verify the variable is set correctly.
  • Persist the variable via ~/.bashrc.
  • Unset the variable from the current session.

Concepts

  • PATH controls executable lookup order in the shell and is a primary cause of “command not found.”
  • Environment variables are inherited by child processes and can change tool behavior without changing command syntax.
  • export marks a shell variable for inheritance by processes launched from the session.
  • Startup files like ~/.bashrc define per-user session defaults for interactive shells.
  • unset removes a variable from the current session, helping you confirm whether values come from startup files or runtime changes.

Walkthrough

Step 1 : Inspect the current PATH value.
Command
echo $PATH

PATH controls where the shell searches for executables. If a user reports missing commands or inconsistent tool versions, verify PATH early and confirm expected directories are present and ordered correctly.

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Step 2 : Enumerate the current environment.
Commands
printenv
# OR
env

printenv and env provide a snapshot of session state. This is how you confirm what values are actually inherited by processes launched from the shell.

SHELL=/bin/bash
USER=lab
LOGNAME=lab
HOME=/home/lab
PWD=/home/lab
HOSTNAME=env-lab
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG=en_US.UTF-8
...
Step 3 : Create and export a project directory variable.
Command
export PROJECT_DIR=/srv/app

export sets the variable in the current shell and ensures child processes inherit it. Use this when a program, script, or tool needs a consistent session-level value.

Step 4 : Verify the variable value.
Command
echo $PROJECT_DIR

Verify immediately after setting the value. This confirms the shell holds the expected path and reduces time spent debugging typos later.

/srv/app
Step 5 : Persist the variable for new interactive shells.
Command
echo 'export PROJECT_DIR=/srv/app' >> ~/.bashrc

Appending to ~/.bashrc ensures the variable is set each time an interactive shell starts. Persist only when the user needs the value across sessions.

Step 6 : Unset the variable from the current session.
Command
unset PROJECT_DIR

unset removes the variable from the current shell session. This helps you test behavior without the value and confirm whether startup files reintroduce it on new shells.

Common breakpoints

PATH is missing expected directories

If core paths like /usr/bin or /bin are missing, the shell may fail to find standard tools. Look for overrides in ~/.bashrc, ~/.profile, or system-wide shell configuration.

PROJECT_DIR works now but not in a new session

The variable was set for the current shell only. Persist it in the correct startup file and start a new interactive shell to confirm it is applied.

Duplicate exports accumulate in ~/.bashrc

Re-appending the same line creates multiple definitions that are harder to audit. Keep one export line per variable and remove duplicates during cleanup.

unset does not “fix” a broken tool

Unsetting only affects the current session. If behavior is coming from a startup file or a system service environment, correct the source and validate in a fresh shell or process.

Cleanup checklist

Remove the persistent export if it was only needed for testing, then confirm the variable is not present in a new shell.

Commands
grep -n 'PROJECT_DIR' ~/.bashrc
# Remove the line(s) manually if present, then:
unset PROJECT_DIR
Success signal

echo $PROJECT_DIR is empty in the current shell, and new shells do not reintroduce the variable unless you intentionally persist it.

Reference

  • echo $PATH : Prints the PATH value used for command lookup.
  • printenv : Prints environment variables for the current session.
  • env : Displays the current environment.
  • export <name>=<value> : Sets a variable in the current shell and exports it to child processes.
  • echo $<name> : Prints the current value of an environment variable.
  • echo 'export <name>=<value>' >> ~/.bashrc : Persists a variable for new interactive shells.
    • >> : Appends to the file without overwriting existing content.
  • unset <name> : Removes a variable from the current shell session.
  • grep -n <pattern> <file> : Searches a file for matching lines and shows line numbers.
    • -n : Prints line numbers for matches.