Loading...

Lab 20: Manage Environment Variables

Inspect the current shell environment, set variables for the active session, and make changes persist across login shells. Update PATH safely and identify the correct per-user and system-wide startup locations.

core troubleshooting services

Scenario

A teammate reports that a tool works in one terminal session but “disappears” in a new login. You need to validate the current environment, export variables correctly, and make changes persist for bash login shells without breaking existing PATH behavior. You also need to know where to place global environment settings for all users.

Operator context

Most “it works on my machine” issues in Linux come down to startup files, PATH ordering, and whether the shell is login vs non-login. Treat env changes like config changes: verify, apply, re-check in a new session.

Objective

  • Display all environment variables in the current shell.
  • Set a new exported variable for the current session.
  • Identify the correct per-user file for bash login-shell persistence.
  • Append a directory to PATH safely for the current session.
  • Identify the system-wide file used to define global environment variables.

What You’ll Practice

  • Environment inspection with printenv and env.
  • Shell variable scope: session-only vs exported environment variables.
  • bash startup behavior for login shells (~/.bash_profile, ~/.bash_login, ~/.profile).
  • Safe PATH manipulation with export PATH="$PATH:/new/path".
  • System-wide environment configuration via /etc/environment.

Walkthrough

Step 1 : Show all environment variables for the current shell.
Command
printenv
# OR
env

Both printenv and env display the exported environment for the current process context. This is the fastest way to confirm what the shell will pass to child processes.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/home/lab
SHELL=/bin/bash
Step 2 : Set a temporary environment variable for this session.
Command
export FOO=bar

export marks the variable for inheritance by child processes. Without export, the variable exists only as a shell variable in the current shell.

Variable FOO set to 'bar'.
Step 3 : Make the variable persistent for bash login shells.
File
~/.bash_profile
# OR (depending on system config)
~/.bash_login
~/.profile

bash login shells typically read ~/.bash_profile first. If it does not exist, bash may fall back to ~/.bash_login or ~/.profile depending on how the system is configured. Adding export FOO=bar there makes it available in new login sessions.

# Add a line like:
export FOO=bar
Step 4 : Append /opt/scripts to PATH for this session.
Command
export PATH=$PATH:/opt/scripts

Always preserve the existing PATH by referencing $PATH. Appending adds your directory at the end, which reduces the risk of accidentally shadowing system binaries with a custom script of the same name.

PATH updated for this session.
Step 5 : Identify the system-wide environment configuration file.
File
/etc/environment

/etc/environment is commonly used to define global environment variables at login for all users. It is not a shell script. Keep the format simple (KEY=VALUE) and avoid shell syntax.

/etc/environment is used for global env vars.
Step 6 : Validate changes in a fresh session.
Command
# Start a new login shell and confirm:
printenv FOO
echo $PATH

Session changes prove the syntax works. A new login session proves persistence and correct startup-file placement. This is the standard “done means verified” check for environment troubleshooting.

# Expected:
bar
# PATH should include:
/opt/scripts

Reference

  • printenv : Displays exported environment variables.
  • env : Displays the environment and can run a command with a modified environment.
  • export NAME=value : Creates or updates an environment variable for the current shell and its child processes.
  • ~/.bash_profile, ~/.bash_login, ~/.profile : Per-user startup files typically read by bash login shells (selection depends on what exists and system config).
  • /etc/environment : System-wide environment variables for all users at login (KEY=VALUE format, not shell code).
  • PATH : Controls command lookup order; preserve existing values when modifying ($PATH).