Files
dotfiles/local/bin/x-path-prepend

34 lines
671 B
Bash
Executable File

#!/usr/bin/env bash
#
# Add a directory to the front of the PATH if it exists and is not already there
# Usage: x-path-prepend <dir>
# Set verbosity with VERBOSE=1
VERBOSE="${VERBOSE:-0}"
# Function to print messages if VERBOSE is enabled
# $1 - message (string)
msg()
{
[[ "$VERBOSE" -eq 1 ]] && echo "$1"
}
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <dir>"
exit 1
fi
dir="$1"
if [ ! -d "$dir" ]; then
msg "(?) Directory $dir does not exist"
exit 0
fi
if echo "$PATH" | grep -qE "(^|:)$dir($|:)"; then
msg "(!) Directory $dir is already in PATH"
else
export PATH="$dir${PATH:+":$PATH"}"
msg "(!) Directory $dir has been added to the front of PATH"
fi