Skip to main content
RunBook Academy

LinuxII · Shell and Command-Line OperationsEnvironment

Environment variables, PATH, and shell initialisation

Foundation⏱ ~10 minbashenvprintenvexport

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

Not yet marked complete on this device.

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 (no export). 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.
Read-only / Safelocal vs exported
$ MY_LOCAL=hello; export MY_EXPORTED=world; bash -c 'echo MY_LOCAL=$MY_LOCAL; echo MY_EXPORTED=$MY_EXPORTED'
MY_LOCAL=
MY_EXPORTED=world

Illustrative 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.

Read-only / SafePATH
$ echo "$PATH" | tr ':' '
'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin

Illustrative 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/sbin and /usr/bin on modern systems.

Shell initialisation files

When a bash shell starts, it reads a sequence of files depending on how it was invoked:

FileRead when
/etc/profileevery login shell
/etc/profile.d/*.shevery login shell (sourced by /etc/profile)
/etc/bash.bashrcevery interactive non-login shell (Debian-family convention)
~/.bash_profilelogin shell (if it exists)
~/.bash_loginlogin shell (if .bash_profile does not exist)
~/.profilelogin shell (if neither .bash_profile nor .bash_login exist)
~/.bashrcevery 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.

Read-only / Safeshell startup files
$ 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 ~/.profile

Illustrative output

Systemd unit environment

Services started by systemd do not read ~/.bashrc. They get their environment from:

  1. The unit file’s Environment= and EnvironmentFile= directives.
  2. The system manager’s default environment (/etc/default/ on Debian-family, /etc/sysconfig/ on RHEL-family).
  3. The manager-environment generator, which copies a curated subset of /etc/environment into 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

  1. Q1. You set `FOO=bar` (no export) in shell A, then `bash` to start shell B. In shell B, what does `echo $FOO` print?

  2. Q2. PATH order matters: a matching executable in a directory earlier in PATH shadows one in a later directory.

  3. 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.