Last active
September 3, 2025 09:45
-
-
Save githubhjs/56c97d7107715314fb85abfff9870fec to your computer and use it in GitHub Desktop.
This Zsh function, dedup_env_var, is designed to remove duplicate entries from a colon-separated environment variable, such as $PATH.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/zsh | |
| # | |
| # Function: Removes duplicate, colon-separated entries from a specified | |
| # environment variable. | |
| # Usage: dedup_env_var <VARIABLE_NAME> | |
| # Example: dedup_env_var PATH | |
| # | |
| dedup_env_var () { | |
| # Store the first argument (the environment variable's name) in a local variable. | |
| local var_name="$1" | |
| # Check if the target environment variable is unset or empty. | |
| # (P)var_name is Zsh's parameter indirection, which gets the value of the | |
| # variable whose name is stored in the var_name variable. | |
| if [ -z "${(P)var_name}" ]; then | |
| # Print the error message to standard error (stderr). | |
| echo "Error: Environment variable '$var_name' is not set." >&2 | |
| return 1 | |
| fi | |
| # Declare a local (-a for array) array with unique (-U) elements. | |
| # This is a more explicit way of writing 'typeset -U array'. | |
| local -aU array | |
| # This is the updated, robust line: | |
| # 1. `${(P)var_name}`: Gets the content of the environment variable (e.g., the value of $PATH). | |
| # 2. `(@s/:/)`: Splits the string by colons. The `@` flag ensures proper array | |
| # treatment and preserves empty fields. | |
| # 3. `("...")`: The outer double quotes prevent word splitting, ensuring that | |
| # paths with spaces are treated as a single, complete element. | |
| # 4. Because 'array' was declared as unique (-U), duplicates are automatically | |
| # removed during this assignment. | |
| array=("${(@s/:/)${(P)var_name}}") | |
| # `(j/:/)` joins the array elements back into a single string, separated by colons. | |
| # Finally, export the cleaned-up string, overwriting the original environment variable. | |
| export "$var_name"="${(j/:/)array}" | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Of course. Here is an explanation of the Zsh function in English.
This Zsh function, dedup_env_var, is designed to remove duplicate entries from a colon-separated environment variable, such as $PATH.
It's a useful utility for cleaning up variables that may have accumulated redundant paths after sourcing multiple configuration files.
Line-by-Line Code Analysis
Here is a breakdown of what each line in the function does:
Bash
dedup_env_var () {
# Declares a local variable 'var_name' and assigns it the value of the
# first argument passed to the function.
# For example, if you run
dedup_env_var PATH, then $var_name becomes "PATH".local var_name="$1"
}
Explanation of the Commands in the Screenshot
which dedup_env_var: The user runs which to find the location of the dedup_env_var command. Since it is a shell function and not an executable file in the system's PATH, the shell responds by printing the function's source code.
dedup_env_var PATH: The user then calls the function, passing PATH as the argument. This triggers the function to:
Read the current value of the $PATH environment variable.
Split it into an array, removing any duplicate path entries in the process.
Join the unique paths back together into a single, colon-delimited string.
Export the cleaned-up string as the new value for the $PATH variable.
Summary
In essence, dedup_env_var is an elegant and efficient Zsh script that leverages Zsh's advanced parameter expansion and unique array features to clean up environment variables.