mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-01-26 03:04:06 +00:00
42 lines
1.1 KiB
Bash
Executable File
42 lines
1.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Optimized script to remove directories from PATH.
|
|
# For each specified directory, all occurrences are removed from PATH.
|
|
#
|
|
# Usage: x-path-remove <directory1> [<directory2> ...]
|
|
#
|
|
# Enable verbose output by setting the environment variable VERBOSE=1.
|
|
#
|
|
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
|
# License: MIT
|
|
|
|
VERBOSE="${VERBOSE:-0}"
|
|
|
|
# Ensure that at least one directory is provided.
|
|
[ "$#" -lt 1 ] && {
|
|
echo "Usage: $0 <directory> [<directory> ...]"
|
|
exit 1
|
|
}
|
|
|
|
for dir in "$@"; do
|
|
# Remove trailing slash if present, unless the directory is "/"
|
|
[ "$dir" != "/" ] && dir="${dir%/}"
|
|
|
|
# Check if the directory is present in PATH.
|
|
case ":$PATH:" in
|
|
*":$dir:"*)
|
|
# Remove all occurrences of the directory from PATH using parameter expansion.
|
|
PATH=":${PATH}:"
|
|
PATH="${PATH//:$dir:/:}"
|
|
PATH="${PATH#:}"
|
|
PATH="${PATH%:}"
|
|
[ "$VERBOSE" -eq 1 ] && echo "Removed '$dir' from PATH."
|
|
;;
|
|
*)
|
|
[ "$VERBOSE" -eq 1 ] && echo "(?) '$dir' is not in PATH."
|
|
;;
|
|
esac
|
|
done
|
|
|
|
export PATH
|