Loading...

Lab 55: Environment Variables

Inspect and modify environment variables to troubleshoot user issues caused by misconfigured session state. Validate PATH, enumerate the current environment, create and verify a custom variable, persist it via shell startup files, and remove it cleanly.

troubleshooting core users

Scenario

You are troubleshooting a user issue related to PATH and custom environment variables. The user reports “commands not found” behavior and inconsistent application paths between sessions. Your job is to inspect the current environment, validate PATH, add a project variable for a deployment directory, make it persistent, then remove it safely.

Operator context

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

Objective

  • Print the current PATH value.
  • List all environment variables in the current session.
  • Create a new environment variable PROJECT_DIR.
  • Verify the variable is set correctly.
  • Persist the variable across sessions via ~/.bashrc.
  • Unset the variable from the current session.

What You’ll Practice

  • Reading variables safely using echo.
  • Enumerating the current environment using env or printenv .
  • Creating and exporting variables using export .
  • Persisting variables in a user shell profile (~/.bashrc).
  • Removing variables from the current session using unset.

Walkthrough

Step 1 : View the current value of PATH.
Command
echo $PATH

PATH controls where the shell searches for executables. If a user reports “command not found,” validate PATH early to confirm the expected directories are present and ordered correctly.

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Step 2 : List all currently defined environment variables.
Command
printenv
# OR
env

printenv and env provide a snapshot of session state. This helps you catch unexpected overrides and confirms 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 a new environment variable for a project path.
Command
export PROJECT_DIR=/srv/app

export sets the variable in the current shell and ensures child processes inherit it. This is the standard pattern when you need an application or script to see a session-level value.

Step 4 : Confirm PROJECT_DIR is set.
Command
echo $PROJECT_DIR

Verify immediately after you set the variable. This confirms there were no typos and the shell is holding the expected value.

/srv/app
Step 5 : Persist the variable across sessions.
Command
echo 'export PROJECT_DIR=/srv/app' >> ~/.bashrc

Appending to ~/.bashrc ensures the variable is set each time an interactive shell starts. This is useful when a user relies on the variable for daily workflow.

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

unset removes the variable from the current shell session. This is useful when testing behavior and verifying whether a value is being pulled from startup files or set dynamically.

Reference

  • echo $PATH : Prints the current PATH value used for command lookup.
  • printenv : Prints environment variables for the current session.
  • env : Displays the current environment; also commonly used to run a command with a modified environment.
  • export NAME=value : Sets a variable in the current shell and exports it to child processes.
    • Use this when a value must be inherited by a program started from the shell.
  • echo $NAME : Prints the current value of an environment variable.
  • echo 'export NAME=value' >> ~/.bashrc : Appends a variable export to ~/.bashrc so it is set automatically in new interactive shells.
    • >> appends to the file without overwriting existing content.
  • unset NAME : Removes a variable from the current shell session.