The PATH Environment Variable

The PATH Environment Variable

When you check how Linux finds a command, the order of directories in PATH helps explain which executable the shell selects. PATH is a list of directories that Bash searches from left to right when a command has no slash.

This article shows how to check the active value and choose where to save a change for later shell sessions.

TL;DR: PATH tells the shell where to find commands

PATH is an ordered, colon-separated list of directories that a shell searches when you enter a command without a slash.

  • Print it with printf ‘%s\n’ “$PATH”.
  • Add a directory for this shell with export PATH=”$HOME/bin:$PATH”.
  • Save the line in a startup file read by the shell that needs it.

What is the PATH environment variable?

PATH names directories that contain programs. When you enter a command without a slash, Bash checks functions and builtins before searching PATH from left to right.

Unix-like shells separate entries with colons, while Windows uses semicolons. DOS and Windows use their own command interpreters and environment rules, so Linux instructions should use the target shell’s syntax. A command containing a slash, such as ./script.sh, names its location directly rather than triggering a PATH search.

Bash handles builtins such as cd itself, while PATH locates executable files that need execute permission.

I ran bash path_demo.sh and got this output:

Current PATH: /usr/local/bin:/usr/bin:/bin
sh resolves to: /usr/bin/sh
After adding $HOME/bin at the front: /home/ubuntu/bin:/usr/local/bin:/usr/bin:/bin
Terminal showing PATH entries and the resolved sh command
PATH entries are colon-separated, and command -v reports the resolved executable.

The first matching executable in the search order wins, so putting a directory first can select a different program with the same name. After replacing a command, run hash -r to clear Bash’s remembered location.

A terminal receives its environment when it starts. Separate terminals can show different values if their parent sessions or startup files differ.

How to inspect and update PATH in Linux

Inspect the active value, resolve a command, and then choose whether a new directory should apply only now or in later shell sessions.

Step 1: Print PATH and check a command

Use a quoted expansion to print the whole value without accidental word splitting. Use command -v to ask the current shell how it would resolve a name.

printf '%s\n' "$PATH"
command -v sh

In my comparison, command -v sh and type -P sh both returned /usr/bin/sh. I use command -v when I need the current shell’s resolution, including functions and builtins.

Step 2: Add a directory for the current shell

Prepend a directory when its executable should take precedence, or append it when existing matches should remain ahead. Quoting protects spaces in directory names, and retaining the old value avoids replacing every existing search entry.

export PATH="$HOME/bin:$PATH"
# Or append it instead:
export PATH="$PATH:$HOME/bin"
command -v my-tool

Replace my-tool with an executable that exists in the selected directory. This export changes the current shell and reaches programs launched from it.

Step 3: Choose a startup file for lasting changes

Choose the file that matches the target shell session.

Shell contextCommon fileUse
Interactive non-login Bash~/.bashrcInteractive terminal settings
Login Bash~/.bash_profile, ~/.bash_login, or ~/.profileFirst available file in Bash’s login-file order
Interactive Zsh~/.zshrcZsh interactive settings

Bash reads the first available login file, and it does not load .bashrc automatically from that file. See the LinuxForDevices guide to Bash profile and bashrc files before editing.

Interactive shell startup files do not necessarily configure background services or scheduled jobs. Set PATH with the mechanism that launches each process.

Step 4: Verify the result

Check both the variable and the command after making a change. Looking at PATH alone confirms that text was added, while resolving the executable confirms which file the shell selects.

printf '%s\n' "$PATH"
command -v my-tool

If a program was replaced at its old location and Bash still names that path, run hash -r and check again. That clears Bash’s remembered command locations, not the PATH value itself.

PATH mistakes and safe fixes

A failed lookup, missing startup change, or unexpected executable usually points to a specific PATH or shell-state issue. Check the active value before changing more files. There is no single default PATH for every Linux distribution or account, so compare the current value with the shell and distribution documentation rather than pasting a sample list over it.

SymptomLikely causeFix
Command not foundIts directory is absent, the shell has not loaded the change, or the file is not executableInspect PATH, confirm the file and permissions, then start or reload the correct shell configuration
Other commands stop resolvingAn assignment replaced PATH instead of preserving its old valueOpen a fresh terminal to recover its inherited value, then prepend or append while retaining $PATH
Wrong version runsAn earlier directory contains a same-named executableUse command -v name and move the intended directory earlier only when it is trusted
Change vanishes in a new terminalThe export was only run in the old shell or saved in a file that this shell does not readChoose the matching startup file and verify in a newly started shell

I ran bash -c ‘command -v jd_path_missing_demo’. Bash printed no resolved path and returned status 1.

I use command -v first because it checks how the current shell resolves a name. Then check spelling, PATH entries, file presence and executable permission.

Do not prepend the current directory with a dot as a routine fix. A writable directory early in PATH can expose a privileged script to unintended commands. Use full executable paths in privileged scripts and keep PATH directories controlled by trusted owners.

The variable name is uppercase and case-sensitive. A lowercase variable named path does not replace PATH, and a correct-looking value can still point to a directory without the intended executable.

When an assignment overwrites the list, inspect the startup file that changed it and restore the previous entries there. Linux distributions and accounts can build different defaults, so avoid pasting a sample PATH from another machine. Use a known backup or the distribution’s own documentation, then start a new matching shell to confirm the correction.

Conclusion

Adding a directory early changes command priority, while appending it leaves earlier matches in control. If a command is absent from PATH, its full path can still run the executable directly when it exists and is permitted.

For related tasks, read the LinuxForDevices guide to setting environment variables in Linux and its guide to Linux shell types. Bash’s official manual documents command search and execution and startup files.

FAQ

These short answers cover common PATH questions that arise after the first edit.

What separates directories in Linux PATH?

A colon separates each directory in a Unix-like PATH value. A semicolon is used by Windows PATH values.

Does exporting PATH make the change permanent?

No. Export changes the current shell and its child processes. Save the export in the appropriate startup file for later shells.

Does the order of PATH directories matter?

Yes. The shell searches in order and normally selects the first matching executable it finds.