PATH in Bash and Fish Shell

Updated 28-Jun-2024

PATH can be a pain, but there are some basics:

  • Syntax
    • $PATH before or after (before)
    • Is EXPORT needed, or not (not)
  • Where is PATH set
    • All apps+shells /etc/environment
    • All shells ~/.profile or /etc/profile for all users (or a separate file under /etc/profile.d/ if using pacman)
    • Bash shell ~/.bashrc (or ~/.bash_profile or ~/.bash_login)
    • Fish shell ~/.config/fish/config.fish

To Export or Not

Export is useful in a script, when say making a copy of the path, changing the path, exporting (updating the live path), executing some other commands, and then changing the path back and re-exporting (see example here). However, export doesn't work the same way in all shells.

However, simply having permanent Export commands in a standard config file makes no sense, though it is common practice.

Where to put the PATH variable

> bash as a login shell doesn't parse ~/.profile if either ~/.bash_profile or ~/.bash_login exists. -Source

Also, note that ~/.bash_profile is not run if there is a non-login shell invocation, which means ~/.bashrc is really where you want PATH to be (for Bash).

~/.bashrc vs. ~/.bash_profile vs. ~/.profile

  • ~/.bashrc only works for shells that are both non-login and interactive, though this is largely ignored as most ~/.bash_profiles load ~/.bashrc by default.
  • ~/.profile is the old /bin/sh login script, and Bash will read it to be backward compatible
  • ~/.bashrc is generally good, for odd/unknown reasons it fails to load at times, especially second and subsequent logins.

Environment

If a single PATH should be the same for every shell, and even every app, then a core environmental variable should be the place where the PATH would reside.

Previous to all ./profiles is the use of /etc/environment which is global.

Bash ~/.bashrc Example

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias lx='ls -la --color=auto'
alias rx='rm -rf'
PATH=$PATH:$HOME/bin:~/.local/bin:/usr/local/bin
export PATH

Fish ~/.config/fish/config.fish Example

set PATH $PATH /usr/local/bin
export PATH
function ll
    ls -lh $argv
end

See also: Fish Shell $PATH

Reload the Config file (while preserving the session)

  • Bash: source ~/.bashrc
  • Fish: source ~/.config/fish/config.fish