Linux online course

Linux PATH Environment Variable: Command Search Paths and Usage

Learn what the Linux PATH environment variable does, how shells search it, how to run scripts, change PATH safely, and troubleshoot command lookup.

PATH is a Linux environment variable that tells a shell where to look for executable commands. It is one of the most important settings for using the terminal because it lets you type a command such as ls from almost any working directory without typing the program's full filesystem location.

What PATH Does

An environment variable is a named value inherited by processes and used to influence program behavior. The shell is the command interpreter, such as Bash, that reads commands and starts programs.

When you enter a command name without a slash, the shell uses the PATH environment variable to search for an executable file. PATH contains an ordered list of directories. The shell examines those directories from left to right until it finds an executable with the requested name.

For example, when you type:

ls

the shell may find the program in /usr/bin/ls. You do not need to type /usr/bin/ls because /usr/bin is normally one of the directories in PATH.

PATH concerns command lookup, not the directory where a command's input or output is located. Your working directory is the directory in which the shell is currently operating. A program can be found through PATH even when your working directory is somewhere else.

PATH Format

PATH is a colon-separated list: each directory is separated from the next by a colon character (:).

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

In this example, the search order is:

  1. /usr/local/bin
  2. /usr/bin
  3. /bin
  4. /usr/sbin
  5. /sbin

Directory order matters. If two directories contain executable files with the same name, the executable in the earlier directory wins. This rule is called PATH precedence.

PATH entries are directories, not individual command files. A typical entry such as /usr/bin means that the shell may search for many command names inside that directory.

Displaying Your Current PATH

Use echo to print the PATH value in the current shell:

echo "$PATH"

The printenv command provides an alternative way to inspect an environment variable:

printenv PATH

Because a PATH value can be long, display one directory per line for easier reading:

printf '%s\n' "$PATH" | tr ':' '\n'

This makes it easier to check whether a directory is present and determine its position in the search order.

How Command Lookup Works

When a command is entered without a slash, the shell generally performs these steps:

  1. It interprets the command line and handles shell features such as aliases, functions, and built-in commands when applicable.
  2. For an external command name, it checks the PATH directories from left to right.
  3. It looks for a matching executable file in each directory.
  4. It runs the first matching executable it can use.
  5. If no matching executable is found, it reports a command-not-found style error.

For example, if PATH is:

/home/user/bin:/usr/local/bin:/usr/bin

and both /home/user/bin/tool and /usr/bin/tool exist and are executable, entering tool runs /home/user/bin/tool.

A command containing a slash is handled differently. The shell uses the path you supplied rather than searching PATH. Thus ./tool and /usr/bin/tool do not ask PATH to locate tool.

Running Scripts from a PATH Directory

A script can be run by its filename from another working directory when its containing directory is in PATH, the file has execute permission, and the system knows which interpreter should run it.

For a shell script, the first line commonly contains a shebang, which is an interpreter directive beginning with #!:

#!/bin/sh
echo "Hello from my script"

Suppose the file is saved as $HOME/bin/hello-script. Create the directory and make the file executable:

mkdir -p "$HOME/bin"
chmod +x "$HOME/bin/hello-script"

For the shell to find it by name, add that directory to PATH for the current session:

export PATH="$HOME/bin:$PATH"

Now change to a different directory and run the script by filename:

cd /tmp
hello-script

The script's location is $HOME/bin, while the working directory is /tmp. PATH connects the command name to the script location; it does not change the working directory.

Running Files Outside PATH

Linux does not normally search the current directory automatically. If script.sh is in the current directory but that directory is not in PATH, this usually fails:

script.sh

The shell interprets this as a request to find an executable named script.sh in the PATH directories. It does not automatically look in the working directory.

Use ./ to explicitly identify the current directory:

./script.sh

Here, ./script.sh is a relative path. It means “the file named script.sh relative to the current working directory.” The file must still have execute permission and, when directly run as a script, a usable shebang:

chmod +x script.sh
./script.sh

You can also use an absolute path, which is a complete filesystem location beginning with /:

/home/user/script.sh

Both ./script.sh and /home/user/script.sh contain a slash, so the shell uses the supplied path instead of performing a normal PATH search.

Invocation formExampleUses PATH searchWhen it works
Bare command namescript.shYesThe script's directory is in PATH, and the file is executable.
Relative path./script.shNoThe file exists in the current directory and is executable.
Absolute path/home/user/script.shNoThe file exists at that exact location and is executable.
Interpreter explicitly selectedsh script.shNo for the script fileThe selected interpreter can read the script; execute permission is not required for this form.

The Current Directory and Security

. is a path notation meaning the current directory. Therefore, ./script.sh explicitly runs a file from the current directory.

Adding . to PATH can make bare command execution appear convenient:

export PATH=".:$PATH"

However, placing the current directory before trusted system directories creates a security risk. A malicious or accidental file named ls, sudo, or another common command could run when you intended to invoke the system command. The current directory also changes as you move around, so the command found by the same name may change unexpectedly.

For these reasons, typical Linux configurations do not include . in PATH. Use an explicit path such as ./script.sh when you intend to run a file from the current directory.

PATH Precedence and Command Name Collisions

Different directories can contain executables with the same name. For example:

/home/user/bin/report
/usr/local/bin/report

If PATH is:

/home/user/bin:/usr/local/bin:/usr/bin

then report resolves to /home/user/bin/report. If you reverse the first two entries, the /usr/local/bin version takes precedence.

Use command -v to see the command location that the current shell would select:

command -v report

Use type -a to show multiple matching locations where supported:

type -a report

These tools are useful when a command behaves differently from what you expect. They can also reveal that a name refers to a shell alias, function, or built-in rather than an external executable.

Temporary PATH Changes

Use export to set a PATH value for the current shell and processes started from it. To prepend a personal command directory:

export PATH="$HOME/bin:$PATH"

To append it instead:

export PATH="$PATH:$HOME/bin"

Prepending gives your directory higher precedence than later entries. It is useful when you deliberately want your personal version of a command to override a system version. Appending gives existing system directories precedence and is often safer when you only want to make additional commands available.

These changes normally last until the shell exits. They are inherited by programs launched from that shell, but they do not automatically update already-running unrelated terminals or graphical applications.

GoalCommand patternScopeNotes
Prepend a directoryexport PATH="$HOME/bin:$PATH"Current shell and child processesPersonal commands take precedence over later matching names.
Append a directoryexport PATH="$PATH:$HOME/bin"Current shell and child processesExisting PATH entries remain preferred.
Inspect the resultprintf '%s\n' "$PATH" | tr ':' '\n'Current shellCheck both presence and order.
Refresh Bash lookup datahash -rCurrent Bash shellUseful after changing PATH or replacing an executable.

Persistent PATH Changes

To make a PATH change available in future shells, place the export command in an appropriate shell startup file. A startup file is a configuration file read when a shell starts.

Bash interactive shells

For Bash interactive shells, a personal addition is commonly placed in ~/.bashrc:

export PATH="$HOME/bin:$PATH"

After saving the file, reload it in the current shell:

source ~/.bashrc

Bash login shells

Login shells use login initialization files. Depending on the system and Bash configuration, an appropriate file may be ~/.profile or ~/.bash_profile:

export PATH="$HOME/bin:$PATH"

Do not assume that every shell reads every startup file. Interactive and login shells have different initialization behavior, and a graphical session may establish its environment through desktop-session configuration rather than an interactive terminal file.

System-wide environment configuration also varies by distribution and session type. Files managed by the operating system may affect login services, display managers, terminals, or other processes differently. For a personal command directory, a user startup file is generally less disruptive than editing system-wide configuration.

PlacementResolution behaviorTypical useRisk or consideration
Before system directoriesPersonal or local executable wins collisions.Testing a newer command or using a user-specific version.Unexpected overrides and greater trust requirements.
After system directoriesExisting system executable wins collisions.Adding extra personal utilities without overriding standard commands.A desired personal command may not be selected if names collide.
Current directory via .Files in the current directory may be selected by bare name.Generally avoid; use explicit relative paths instead.Security risk, especially before trusted directories.
Directory absent from PATHBare names in that directory are not resolved.Running files with explicit relative or absolute paths.Commands are less convenient by name, but lookup is explicit.

Safe PATH Management and Verification

Always preserve the existing PATH when adding a directory. This is safe:

export PATH="$HOME/bin:$PATH"

This is potentially harmful:

export PATH="$HOME/bin"

The second command replaces PATH entirely. Standard directories may disappear, causing commands such as ls or administrative tools to stop being found.

After changing PATH, verify both the value and command resolution:

printf '%s\n' "$PATH" | tr ':' '\n'
command -v hello-script
type -a hello-script

Only add directories you trust and control. A directory in PATH can contain programs that will run when their names are entered. Avoid writable-by-everyone directories and be especially cautious when a PATH is used by privileged programs.

Bash can cache command locations. If you add a new executable or replace one with another version and Bash continues to use an old location, refresh its command lookup cache:

hash -r

Then check again with command -v or type -a.

Troubleshooting PATH Problems

“Command not found” for a script

Likely causes include a missing script directory in PATH, the current directory not being searched, or an incorrect command name.

  1. Print PATH and compare its entries with the script's directory.
  2. From the script's directory, try ./script.sh.
  3. Try the script's absolute path.
  4. Add its trusted directory to PATH if bare-name execution is needed.

The script is found but cannot be executed

Check its permissions:

ls -l script.sh

If appropriate, add execute permission:

chmod +x script.sh

Also inspect the shebang. It must name an available interpreter, such as #!/bin/sh. Execution can additionally be blocked by filesystem mount restrictions.

A different command runs

Use:

command -v command_name
type -a command_name

An earlier PATH entry may contain another executable with the same name. Reorder PATH deliberately, then run hash -r in Bash if necessary.

The change works in one terminal only

An export command entered at the prompt affects the current shell and its children. It does not automatically affect a new terminal or graphical session. Identify whether the shell is interactive or a login shell, put the export in the appropriate startup file, and start a new session or reload the file.

Standard commands stop working

PATH was probably overwritten instead of extended, or essential system directories were removed. Restore a known-good PATH for the current session, then use an extension such as:

export PATH="$HOME/bin:$PATH"

If even common commands cannot be found, use their absolute paths where known or start a new shell with a corrected environment.

Key Points

  • PATH is an environment variable containing a colon-separated list of directories.
  • The shell searches PATH from left to right for commands entered without a slash.
  • The first matching executable determines the command that runs.
  • The current directory is not normally searched automatically; use ./name explicitly.
  • Scripts called by filename need a PATH directory, execute permission, and usually a valid shebang.
  • Use command -v and type -a to investigate command resolution.
  • Extend PATH with "$PATH" rather than accidentally replacing it.
  • Use trusted directories, avoid putting . before system directories, and refresh Bash's cache with hash -r when needed.

For a concise reference to this subject, see Linux PATH environment variable.