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.
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.
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.
printenv and env.
~/.bash_profile, ~/.bash_login,
~/.profile.
export PATH="$PATH:/new/path".
/etc/environment.
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
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'.
~/.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
/opt/scripts to PATH for this session.
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.
/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.
# 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
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.
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".
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.
/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.
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.
# Start a new login session after removing the test export and confirm:
printenv FOO
echo $PATH
printenv FOO returns no value (or “not found”), and PATH still contains
the expected baseline directories.
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.
KEY=VALUE format (not shell code).PATH: Controls command lookup order.
export PATH="$PATH:/opt/scripts".