Skip to content

Commit

Permalink
Support updating PATH in place
Browse files Browse the repository at this point in the history
`path_prepend` and `path_append` would previously only add a directory
if it wasn't already in the PATH. Now it will move the directory to
the start/end. This allows the PATH to be adjusted more readily.
  • Loading branch information
norm committed Jan 31, 2022
1 parent 58b545e commit 178c511
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
15 changes: 8 additions & 7 deletions etc/bash/defaults/00.path.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#
# Set the default PATH.

path_append '/sbin'
path_append '/bin'
path_append '/usr/sbin'
path_append '/usr/bin'
path_append '/usr/local/bin'
path_append '/usr/local/sbin'
path_prepend "${HOME}/bin"
path_append /usr/local/bin
path_append /usr/local/sbin
path_append /usr/bin
path_append /bin
path_append /usr/sbin
path_append /sbin

path_prepend ${HOME}/bin
3 changes: 3 additions & 0 deletions etc/bash/functions/00.flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ fi
# The current Operating System type (eg. Linux, Darwin, NetBSD)
export HOSTOS=`uname`

# The current machine architecture
export HOSTARCH=`uname -m`

# The current host's name, in short form (without domain name).
export HOST=`hostname -s`

Expand Down
26 changes: 15 additions & 11 deletions etc/bash/functions/00.path.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
#
# Easily extending the PATH and MANPATH.

function strip_path_dir {
local directory="$1"

echo "$PATH" \
| sed -e 's/:/\n/g' \
| grep -v "^${directory}\$" \
| tr '\n' ':' \
| sed -e 's/:$//'
}

function path_append {
local directory="$1"

if [ -x "$directory" ]; then
echo "$PATH" | grep "$directory" &> /dev/null
[ $? -eq 1 ] \
&& PATH="${PATH}:${directory}"
fi

[ -x "$directory" ] \
&& PATH="$(strip_path_dir "$directory"):$directory"
}

function path_prepend {
local directory="$1"

if [ -x "$directory" ]; then
echo "$PATH" | grep "$directory" &> /dev/null
[ $? -eq 1 ] \
&& PATH="${directory}:${PATH}"
fi
[ -x "$directory" ] \
&& PATH="$directory:$(strip_path_dir "$directory")"
}

function manpath_append {
Expand Down
1 change: 1 addition & 0 deletions etc/bash/functions/20.source.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function source_init_tree {

source_if_exists ${dir}/*.sh
source_if_exists ${dir}/os-${HOSTOS}/*.sh
source_if_exists ${dir}/os-${HOSTOS}-${HOSTARCH}/*.sh
source_if_exists ${dir}/arch-${HOSTTYPE}/*.sh
source_if_exists ${dir}/domain-${DOMAIN}/*.sh
source_if_exists ${dir}/host-${HOST}/*.sh
Expand Down

0 comments on commit 178c511

Please sign in to comment.