コードは以下の通りです。
~/.bashrc
function greppath() {
local FOUND=0
local IFS=':'
local DIR
for DIR in ${2}; do
[ "${1}" == "${DIR}" ] && FOUND=1
done
[ ${FOUND} -ge 1 ] && echo "${1}" && return 0 || return 1
}
使用例その1。
~/.bashrc
if [ -d "${HOME}/bin" ] ; then
if ! greppath "${HOME}/bin" "${PATH}" 2>&1 > /dev/null; then
PATH="${PATH}:${HOME}/bin"
fi
fi
使用例その2。
~/.bashrc
if [ -x /usr/local/bin/brew ]; then
COREUTILS="$(/usr/local/bin/brew --prefix coreutils)"
if [ -n "${COREUTILS}" ]; then
GNUBIN="${COREUTILS}/libexec/gnubin"
if ! greppath "${GNUBIN}" "${PATH}" 2>&1 > /dev/null; then
PATH="${GNUBIN}:${PATH}"
fi
unset GNUBIN
fi
unset COREUTILS
fi
~/.bash_profileと~/.bashrcのどちらに書くか、は悩ましいところです、Emacsから呼び出すこともあることを考えると~/.bashrcがよいように思います。