Skip to content

VOL 00 / CH 02 / LESSON 04

2.4 Environment Variables and PATH ​

Prerequisites: Safe File Operations

Estimated time: 12 minutes

Exercise environment: Bash on GNU/Linux; differences in PowerShell are noted at the end

You are ready to continue when: you can determine the origin of a command and explain when variables are passed to child processes

File operations are working. Now change the question: when you type a name at the prompt, why does the shell handle cd itself while it usually searches the disk for python3? Environment variables and PATH explain the difference.

Shell Variables and Environment Variables ​

bash
unset COURSE_STAGE
COURSE_STAGE=workshop
printf 'shell: <%s>\n' "$COURSE_STAGE"
sh -c 'printf "child before export: <%s>\n" "$COURSE_STAGE"'

export COURSE_STAGE
sh -c 'printf "child after export: <%s>\n" "$COURSE_STAGE"'

First unset this exercise variable so an earlier export cannot affect the result. Expect shell: <workshop>, child before export: <>, and child after export: <workshop>. Assignment creates a shell variable. After export, newly started child processes inherit it. Existing processes do not receive the update, and changes made in a child do not propagate back to the parent.

To view a specified value:

bash
printf 'HOME=%s\n' "$HOME"
printf 'PATH=%s\n' "$PATH"

Do not paste the complete env output arbitrarily into public locations, as it may contain access tokens, proxy addresses, or other sensitive configurations.

PATH is Just Part of Command Parsing ​

In Unix shells, PATH is typically a colon-separated list of directories. When looking for an external command, the program in the earlier directory of the PATH is prioritized; however, aliases, functions, built-in commands, and the Shell's command cache may also participate in the parsing process.

bash
type -a cd
type -a printf
type -a python3
command -v python3

type -a explains whether the command is an alias, a function, a built-in command, or one or more external files. which's implementation and behavior are inconsistent across platforms, making it unsuitable as the sole criterion.

Adding Tools to a Temporary Directory ​

bash
workdir=$(mktemp -d)
mkdir -p "$workdir/bin"
cat > "$workdir/bin/workshop-hello" <<'SCRIPT'
#!/bin/sh
printf '%s\n' 'hello from a private tool directory'
SCRIPT
chmod +x "$workdir/bin/workshop-hello"

original_path=$PATH
PATH="$workdir/bin:$PATH"
export PATH
command -v workshop-hello
workshop-hello

Do not place empty PATH fragments, ., or other user-writable directories before system directories; a different program with the same name could run first. When a script needs a specific program, verify its absolute path or use a trusted runtime environment.

Don't Blindly Modify Startup Files ​

The timing of reading ~/.bashrc, ~/.bash_profile, ~/.profile, and ~/.zshrc varies depending on the Shell, whether you're logged in, whether the session is interactive, and the system configuration. $SHELL typically represents the login Shell for the account, but it is not guaranteed to be the current process:

bash
printf 'login shell: %s\n' "$SHELL"
ps -p $$ -o comm=

When you want to persist your configuration: confirm the current Shell, read the corresponding manual, back up the existing files, manually edit a small part, and then open a new terminal to verify. Do not use redirection to overwrite the entire configuration file, and do not append tutorial snippets to all startup files at the same time.

In PowerShell, environment variables are written as $env:NAME; on Windows, PATH is typically separated by semicolons. Bash/Zsh commands cannot be copied and pasted directly.

Clean up this page's exercises:

bash
rm -r -- "$workdir"
PATH=$original_path
export PATH
unset original_path COURSE_STAGE

→ Next: Wildcards and Pipes

Built with VitePress | Software Systems Atlas