From 749d22c17e2e432eaa52bb4d5e968f2169e5383d Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Thu, 12 Dec 2019 15:06:48 +0000 Subject: [PATCH] bash: use addpath to only add paths once --- bashrc | 21 ++++++++++++++++++--- bashrc.d/local/stbbri-sec-1.bash | 3 +++ bashrc.d/node.bash | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/bashrc b/bashrc index dab7806..af5d893 100644 --- a/bashrc +++ b/bashrc @@ -5,6 +5,21 @@ # If not running interactively, don't do anything [ -z "$PS1" ] && return +# Add an entry to PATH, iff it's not already there. +addpath() { + wanted_path=$1 + at_end=$2 + FOUND=$(echo $PATH | grep $wanted_path) + if [ $? -ne 0 ] ; then + echo "Found with grep" + if [ "$at_end" == "" ] ; then + export PATH=$wanted_path:$PATH + else + export PATH=$PATH:$wanted_path + fi + fi +} + # don't put duplicate lines in the history. See bash(1) for more options # don't overwrite GNU Midnight Commander's setting of `ignorespace'. export HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups @@ -69,7 +84,9 @@ xterm*|rxvt*) esac export PROMPT_DIRTRIM=3 -export PATH=$HOME/bin:$PATH:$HOME/opt/bin +addpath $HOME/bin +addpath $HOME/opt/bin end + export IPLAYER_OUTDIR="$HOME/downloads/iplayer" # enable color support of ls and also add handy aliases @@ -123,8 +140,6 @@ fi # echo "Note - Dropbox isn't running!" # fi -export PATH=$HOME/bin:$PATH - [ -f ~/.fzf.bash ] && source ~/.fzf.bash # Load any supplementary scripts diff --git a/bashrc.d/local/stbbri-sec-1.bash b/bashrc.d/local/stbbri-sec-1.bash index 4495ce1..57edb0f 100644 --- a/bashrc.d/local/stbbri-sec-1.bash +++ b/bashrc.d/local/stbbri-sec-1.bash @@ -3,6 +3,9 @@ if [ -x /usr/bin/dircolors ]; then alias grep='grep --color=auto' fi +# export PATH=$HOME/.local/bin:$PATH +addpath $HOME/.local/bin + # some more ls aliases alias ll='ls -l' alias la="ls -a" diff --git a/bashrc.d/node.bash b/bashrc.d/node.bash index 40dd31b..da8eff8 100644 --- a/bashrc.d/node.bash +++ b/bashrc.d/node.bash @@ -1,5 +1,5 @@ # Set up NPM NPM_PACKAGES="$HOME/.npm-packages" NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH" -PATH=$PATH:$NPM_PACKAGES/bin MANPATH="$MANPATH:$NPM_PACKAGES/share/man" +addpath $NPM_PACKAGES/bin