PATH in Bash and Fish Shell

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

Therefore, since it is likely there are some things are (or will be) in ~/.bash_profile, that would be the most likely place for 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
  • While ~/.bashrc is generally good, for odd/unknown reasons it fails to load at times, especially second and subsequent logins. Therefore, ~/.profile can be a better choice (or ~/.bash_profile).

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
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
PATH=$PATH:/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