Efficiently Manage Your $PATH with a Bash Function

Efficiently manage your $PATH environment variable with this handy Bash function. It prevents duplicates and avoids formatting issues. Add it to your ~/.bashrc:

# Define the pathadd function
pathadd() {
    if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
        PATH="$1${PATH:+":$PATH"}"
    fi
}

# Add paths to $PATH
pathadd "$HOME/tools/riscv/bin"
export GEM_HOME="$(gem env user_gemhome)"
pathadd "$GEM_HOME/bin"

# Clean up
unset pathadd

Sources: