From 807d172521b477f877bf4f9ac025b99a3a864da3 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sun, 15 Apr 2018 20:01:43 +0200 Subject: [PATCH] [bash] fix inconsistent PATH and MANPATH These two major problems existed: * PATH got multiple instances of the same entries for nested shells. * MANPATH got set, but ignoring the results of the system's `manpath` command. This caused some man pages to be missing, because the presence of MANPATH variable prevents man from looking up man pages at the paths returned from `manpath`. Both issues are now fixed :) BONUS: we abstract the last absolute locations we had in terms of where Homebrew is installed. For Linux, Cygwin, or Mac without Homebrew we assume all our custom tools will be installed under /usr/local. It is easy enough to change that in case we have a different directory in a specific system. --- bashrc | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/bashrc b/bashrc index 5a444f2..19e77f1 100644 --- a/bashrc +++ b/bashrc @@ -11,17 +11,39 @@ export LESS="-i -j.49 -M -R -z-2" export PAGER=less export DOTFILES="$HOME/.dotfiles" -# Prepend custom bin directories to PATH if they exist. -for p in /usr/local/opt/coreutils/libexec/gnubin "$DOTFILES/bin" "$HOME/bin" -do - [ -d "$p" ] && export PATH="$p:$PATH" +# Find out where Homebrew performs installations. If Homebrew is not installed +# (e.g. running on Linux), assume /usr/local for our installations. +if command -v brew &>/dev/null; then + BREW_PREFIX=$(brew --prefix) +else + BREW_PREFIX=/usr/local +fi + +# Prevent path_helper from messing with the PATH when starting tmux. +# See: https://superuser.com/a/583502 +if [ -f /etc/profile ]; then + export PATH="" + source /etc/profile +fi + +# Add custom bin dirs to PATH if they exist and are not already in PATH. +for p in $BREW_PREFIX/opt/coreutils/libexec/gnubin $DOTFILES/bin $HOME/bin; do + if [ -d "$p" ] && [[ ":$PATH:" != *":$p:"* ]]; then + PATH="$p:$PATH" + fi done +# If MANPATH is not yet defined, initialize it with the contents of `manpath`. +if [ -z ${MANPATH+x} ]; then + export MANPATH=$(manpath) +fi + # Prepend custom man directories to MANPATH if they exist, so that we get # correct man page entries when multiple versions of a command are available. -for p in /usr/local/share/man /usr/local/opt/coreutils/libexec/gnuman -do - [ -d "$p" ] && export MANPATH="$p:$MANPATH" +for p in $BREW_PREFIX/share/man $BREW_PREFIX/opt/coreutils/libexec/gnuman; do + if [ -d "$p" ] && [[ ":$MANPATH:" != *":$p:"* ]]; then + MANPATH="$p:$MANPATH" + fi done # Useful aliases