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

53 lines
930 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 usage information
usage()
{
echo "Usage: $0 <dir>"
exit 1
}
# Function to print messages if VERBOSE is enabled
# $1 - message (string)
msg()
{
[[ "$VERBOSE" -eq 1 ]] && echo "$1"
}
# Function to add a directory to the front of PATH
# $1 - directory to add (string)
prepend_to_path()
{
local 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
}
# Main function
main()
{
if [ "$#" -ne 1 ]; then
usage
fi
prepend_to_path "$1"
}
main "$@"