#!/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 [ ...] # # Enable verbose output by setting the environment variable VERBOSE=1. # # Author: Ismo Vuorinen 2024 # License: MIT VERBOSE="${VERBOSE:-0}" # Ensure that at least one directory is provided. [ "$#" -lt 1 ] && { echo "Usage: $0 [ ...]" 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