Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pedrovasconcellos/b13bef5f5365f433b7362b5ee6d48937 to your computer and use it in GitHub Desktop.
Save pedrovasconcellos/b13bef5f5365f433b7362b5ee6d48937 to your computer and use it in GitHub Desktop.
Remove duplicates of environment variable PATH
#Checks for the presence of directory /usr/local/go/bin in $PATH.
echo "$PATH" | grep -q -E "(^|:)/usr/local/go/bin($|:)"
is_present=$?
if [ $is_present -eq 1 ]; then
export PATH=$PATH:/usr/local/go/bin
echo "add go bin"
fi
# Cria uma variável temporária para armazenar o novo PATH
new_path=""
# Percorre todos os diretórios no PATH atual
IFS=':' # Define o delimitador como ':'
for dir in $PATH; do
# Verifica se o diretório já foi adicionado ao novo PATH
if ! echo "$new_path" | grep -q -E "(^|:)$dir($|:)"; then
# Se não estiver no novo PATH, adiciona-o
if [ -z "$new_path" ]; then
new_path="$dir"
else
new_path="$new_path:$dir"
fi
fi
done
unset IFS
# Atualiza o PATH com a versão sem duplicatas
export PATH="$new_path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment