Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In POSIX-style shells such as Bash, set PATH with a variable assignment and use export to pass it to commands you run. For example, PATH="/opt/tools/bin:$PATH"; export PATH adds the directory at the front of PATH for the current shell session. set PATH=... is not the portable POSIX or Bash syntax for changing PATH.

Set PATH for your current shell

To add a directory for the commands launched from your current POSIX-style shell, assign the new value and export it:

PATH="/opt/tools/bin:$PATH"
export PATH

This prepends /opt/tools/bin, so the shell searches it before the directories already in PATH. The Open Group’s POSIX.1-2024 Shell Command Language gives export PATH="/local/bin:$PATH" as an example of setting and exporting PATH in one command. Use a directory that exists and that you trust.

To search the new directory after existing entries instead, append it:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
PATH="$PATH:/opt/tools/bin"
export PATH

Prepending gives the new directory priority if more than one directory contains a command with the same name; appending gives existing directories priority. Retaining $PATH preserves the other search locations. POSIX notes that command search is implementation-defined if PATH is unset or null, so replacing it outright may have unexpected results.

Set PATH for only one command

Place the assignment immediately before a command to give that command the modified PATH without changing the shell’s PATH for subsequent commands:

PATH="/opt/tools/bin:$PATH" some-command

The assignment becomes part of that command’s environment. This is useful when a tool needs a different search path for a single run.

Why use export instead of set?

In POSIX shells and Bash, NAME=value assigns a shell variable. export marks it to be included in the environment of subsequently executed commands. The GNU Bash Reference Manual describes export as marking names for inclusion in commands’ environments.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

set has a different role in these shells: it manages shell options and positional parameters, rather than assigning PATH. Bash’s set -a is a special option that makes variables assigned or modified afterward export automatically; it is not needed for an ordinary PATH change.

Make the change persist in future sessions

The commands above change PATH in the current session. To apply a change automatically in future sessions, it must be placed in an appropriate shell startup file. The right file depends on the shell and system, so first identify which shell you use and consult its documentation rather than assuming one startup-file path works everywhere.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

If you use C shell

C shell uses different conventions. Oracle’s C shell documentation describes path and PATH as automatically connected. Do not copy the Bourne-style assignment and export examples above into C shell; use the syntax and startup guidance for your specific C shell implementation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.