LinuxII · Shell and Command-Line OperationsEnvironment
Environment variables, PATH, and shell initialisation
What you'll learn
- Distinguish shell variables from environment variables
- Read and modify PATH safely
- Locate the shell startup files that matter on a production login
- Identify which systemd unit environment-file mechanism applies to services
Prerequisites
Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09
Every running process carries a set of environment variables —
key/value strings inherited from its parent. Misconfigured environment
variables cause an outsized share of production issues: missing
PATH, wrong LANG, forgotten LD_LIBRARY_PATH, and the
infamous EDITOR=vi left over from a personal shell.
Shell variables vs environment variables
Bash has two related concepts that are easy to confuse:
- Shell variable: a name that lives only in the current shell.
Set with
name=value(noexport). Not inherited by child processes. - Environment variable: a shell variable that has been exported
with
export name. Inherited by every child process the shell starts.
$ MY_LOCAL=hello; export MY_EXPORTED=world; bash -c 'echo MY_LOCAL=$MY_LOCAL; echo MY_EXPORTED=$MY_EXPORTED'MY_LOCAL=
MY_EXPORTED=worldIllustrative output
PATH — the most important environment variable
PATH is a colon-separated list of directories. When you type a
command name (not a path), the shell searches each directory in order
and runs the first executable match.
$ echo "$PATH" | tr ':' '
'/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/binIllustrative output
The order is significant. A directory listed earlier in PATH is preferred. Common layout on Debian-family systems:
/usr/local/sbin,/usr/local/bin— locally-installed software, by convention./usr/sbin,/usr/bin— vendor packages./sbin,/bin— legacy directories, often symlinks to/usr/sbinand/usr/binon modern systems.
Shell initialisation files
When a bash shell starts, it reads a sequence of files depending on how it was invoked:
| File | Read when |
|---|---|
/etc/profile | every login shell |
/etc/profile.d/*.sh | every login shell (sourced by /etc/profile) |
/etc/bash.bashrc | every interactive non-login shell (Debian-family convention) |
~/.bash_profile | login shell (if it exists) |
~/.bash_login | login shell (if .bash_profile does not exist) |
~/.profile | login shell (if neither .bash_profile nor .bash_login exist) |
~/.bashrc | every interactive non-login shell |
The most common confusion: ~/.bashrc vs ~/.bash_profile. The rule
of thumb is to put interactive-only configuration (aliases, prompts,
shell functions) in ~/.bashrc, and environment setup (PATH,
EDITOR, LANG) in ~/.profile or ~/.bash_profile. Then have
~/.bash_profile source ~/.bashrc so interactive login shells also
get the rc configuration.
$ ls -la /etc/profile /etc/bash.bashrc ~/.bashrc ~/.profile 2>/dev/null-rw-r--r-- 1 root root 745 Apr 2 2024 /etc/profile
-rw-r--r-- 1 root root 2265 Apr 2 2024 /etc/bash.bashrc
-rw-r--r-- 1 root root 352 Apr 9 11:11 ~/.bashrc
-rw-r--r-- 1 root root 807 Apr 9 11:11 ~/.profileIllustrative output
Systemd unit environment
Services started by systemd do not read ~/.bashrc. They get their
environment from:
- The unit file’s
Environment=andEnvironmentFile=directives. - The system manager’s default environment (
/etc/default/on Debian-family,/etc/sysconfig/on RHEL-family). - The
manager-environmentgenerator, which copies a curated subset of/etc/environmentinto the system manager.
The canonical place for service environment is the unit file’s
EnvironmentFile=, pointing at a file in /etc/default/<service> or
/etc/sysconfig/<service>:
[Service]
EnvironmentFile=/etc/default/myapp
ExecStart=/usr/bin/myapp
The environment file is a simple key=value list, one per line:
MYAPP_PORT=8080
MYAPP_LISTEN=0.0.0.0
Knowledge check
Knowledge check · 3 questions
Q1. You set `FOO=bar` (no export) in shell A, then `bash` to start shell B. In shell B, what does `echo $FOO` print?
Q2. PATH order matters: a matching executable in a directory earlier in PATH shadows one in a later directory.
Q3. Which of the following are correct locations to configure environment for a systemd service? Select all that apply.
Passing score: 75%. Answers are checked in this browser.