Last active
May 5, 2023 02:01
-
-
Save pedrovasconcellos/b13bef5f5365f433b7362b5ee6d48937 to your computer and use it in GitHub Desktop.
Remove duplicates of environment variable PATH
This file contains 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
#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 |
This file contains 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
# 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