How-to

How to Add a Directory to PATH on Linux

PATH is the list of folders the shell searches for commands. Appending a folder is one line; putting it in the right file so every context sees it is the real job.

What is PATH and how is it searched?

echo "$PATH" | tr ':' '\n'
type -a python3
command -v node

PATH is a colon separated list of folders. When you type a command without a slash, the shell checks each folder in order and runs the first match. type -a shows every match in order, which explains why an older binary runs instead of the one you just installed. A typical default on Ubuntu 24.04 starts with /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin.

How do I add a folder for the current session?

export PATH="$PATH:/opt/flyway/bin"       # append, searched last
export PATH="/opt/go/bin:$PATH"           # prepend, searched first
hash -r

Append when the folder adds new commands. Prepend only when you deliberately want your version to win over the system one. hash -r clears bash's cache of command locations so the change takes effect for commands you already ran. Always keep $PATH in the new value; leaving it out wipes the system folders and most commands stop working until you log in again.

How do I make it permanent for my user?

Most distros already add ~/.local/bin and ~/bin to PATH in ~/.profile if those folders exist when you log in. Putting your own scripts there needs no edits at all:

mkdir -p ~/.local/bin
install -m 755 backup-db.sh ~/.local/bin/backup-db

Log out and back in once so the profile sees the new folder. For other folders, add a guarded line to ~/.profile, or ~/.bash_profile on the RHEL family:

case ":$PATH:" in
  *":$HOME/.cargo/bin:"*) ;;
  *) PATH="$PATH:$HOME/.cargo/bin" ;;
esac
export PATH

The case test skips the change if the folder is already present, so sourcing the file twice does not create duplicates.

How do I add it for every user?

sudo tee /etc/profile.d/node22.sh >/dev/null <<'EOF'
case ":$PATH:" in *":/opt/node-v22/bin:"*) ;; *) PATH="/opt/node-v22/bin:$PATH" ;; esac
export PATH
EOF

Files in /etc/profile.d/ run for every login shell. That covers SSH sessions for all users. If you only need one or two binaries available system wide, a symlink into /usr/local/bin is simpler still; see Symbolic links in Linux.

Why does sudo not find my command?

sudo uses its own secure_path instead of your PATH. Check it:

sudo grep -r secure_path /etc/sudoers /etc/sudoers.d/
sudo visudo -f /etc/sudoers.d/path

Add a line such as Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/node-v22/bin". Never add a folder that ordinary users can write to, or anyone could plant a fake command that root then runs. Or call the binary by full path: sudo /opt/node-v22/bin/npm ci.

What about cron jobs and services?

cron starts jobs with a short PATH, usually /usr/bin:/bin. Set it at the top of the crontab:

PATH=/usr/local/bin:/usr/bin:/bin:/opt/flyway/bin
15 2 * * * /home/deploy/bin/nightly-migrate.sh

For systemd services, put Environment="PATH=/opt/node-v22/bin:/usr/local/bin:/usr/bin:/bin" in a drop-in, or simply use full paths in ExecStart. Set environment variables in Linux explains drop-ins.

How do I remove a folder from PATH?

PATH=$(printf '%s' "$PATH" | tr ':' '\n' | grep -vx '/opt/old/bin' | paste -sd:)
export PATH

That rebuilds PATH without the one entry for this session. Then delete the line that added it from your profile file so it does not return.

Is it safe to put . in PATH?

No. A dot means the current folder, so typing ls inside a folder someone else controls could run their program. Run local scripts with ./script.sh instead. The same caution applies to empty entries such as a leading or trailing colon, which also mean the current folder.

How do I confirm which binary runs after a change?

hash -r
type -a terraform
readlink -f "$(command -v java)"

type -a lists every match in PATH order, and the first line is the one bash runs. readlink -f follows symlinks to the real file, which is useful for tools managed through /etc/alternatives. If the wrong version still runs, check for an alias or shell function with the same name, since those win over PATH; type reports them too.

Do zsh and other shells read the same files?

No. zsh reads ~/.zshenv, ~/.zprofile and ~/.zshrc, while dash, the shell behind /bin/sh on Debian and Ubuntu, reads ~/.profile only for login shells. If you switch shells, move your PATH line to the matching file. On FreeBSD, the default root shell is sh and users often run tcsh, which uses setenv PATH in ~/.cshrc instead of export.

Budget x86 and Arm Machines

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E1Intel Xeon E3-1240 v34 / 816 GB DDR32x 240 GB SSD2x 1 Gbps$30.52$91.56 (3 months)
S2Intel Xeon E-2356G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$58.39$58.39 (1 month)
A1Ampere Altra Q80-3080 / 80256 GB DDR42x 2 TB NVMe2x 25 Gbps$252.11$252.11 (1 month)

Keep Reading

Set environment variables in Linux

Set environment variables for one command, a shell session, a user, the whole system or a systemd service, and know which file wins.

Open

Symbolic links in Linux

Create, inspect, update and remove symbolic links with ln -s, use relative targets, and avoid the trailing slash and broken link traps.

Open

The sudo command

How sudo decides who may run what, the flags you use daily, safe sudoers rules, logging and the habits that keep root powers contained.

Open

Set up a cron job

Create a cron job that actually runs: crontab -e, full paths, environment, logging, locking against overlap and mail-free alerts.

Open

FAQ

Quick Answers

Add an export line to ~/.profile for your user, or a file in /etc/profile.d for everyone.

sudo uses secure_path from sudoers. Add the folder there or call the binary by its full path.

Append for new commands. Prepend only to override a system binary on purpose.

cron uses a minimal PATH. Set PATH at the top of the crontab or use full paths.

Test with case ":$PATH:" before adding, as shown above.