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.

Concepts

  • Environment inspection with printenv and env.
  • Variable scope: session-only shell variables versus exported environment variables.
  • bash login startup order: ~/.bash_profile, ~/.bash_login, ~/.profile.
  • Safe PATH manipulation using export PATH="$PATH:/new/path".
  • System-wide environment configuration using /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 exported 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 read ~/.bash_profile first. If it does not exist, bash may fall back to ~/.bash_login or ~/.profile depending on what exists. Add export FOO=bar there to make 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

Preserve the existing PATH by referencing $PATH. Appending places your directory at the end, which reduces the risk of 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

Common breakpoints

Changes work only in the current terminal

If the variable works only in the current session, it was not written into a startup file used by the type of shell you are launching. Confirm whether you are opening a login shell and confirm the correct file is sourced.

PATH overwritten instead of appended

If you set PATH=/opt/scripts without including $PATH, you can break command lookup for standard tools. Always preserve the existing value: export PATH="$PATH:/opt/scripts".

Startup file is not sourced

A non-login interactive shell reads ~/.bashrc, not ~/.bash_profile. If you are using a terminal emulator that launches non-login shells, you may need to source ~/.bashrc from your profile or place exports in the right file for your environment.

Global changes do not apply

/etc/environment applies at login. If you edit it, start a new login session (or log out/in) to confirm the change. Avoid shell syntax in this file.

Cleanup checklist

If you added variables for testing, remove them from the startup file you modified and confirm they no longer appear in a new login session.

Commands
# Start a new login session after removing the test export and confirm:
printenv FOO
echo $PATH
Success signal

printenv FOO returns no value (or “not found”), and PATH still contains the expected baseline directories.

Reference

  • printenv: Displays exported environment variables.
  • env: Displays the environment and can run a command with a modified environment.
  • export NAME=value: Sets 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.
    • ~/.bash_profile: First choice for bash login shells if present.
    • ~/.bash_login: Fallback if ~/.bash_profile does not exist.
    • ~/.profile: Common POSIX fallback file used by multiple shells.
  • /etc/environment: System-wide environment variables for all users at login.
    • Uses simple KEY=VALUE format (not shell code).
  • PATH: Controls command lookup order.
    • Preserve existing values when modifying: export PATH="$PATH:/opt/scripts".