Duplicate system PATH variable (snap/bin:/snap/bin)

When I run echo $PATH the duplicate output snap/bin:/snap/bin appears at the end and seems unusual.

 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

Is this duplication undesired? If so, how can this be changed?


Probably, in your ~/.bashrc or in your ~/.profile there is a command that adds /snap/bin to the $PATH without previously checking if it is still in the $PATH.

Consider that duplicated entries in $PATH variable doesn't have any impacts when you run a command that is in your $PATH. Slowdown may be present on the other hand when you run a command not in $PATH (because the command search is performed, uselessly, two or more times when duplicated entries are present), even though I think it is difficult to perceive this slowdown. Duplicate entries are also a little bit annoying when you want to visually check your $PATH variable.

If you want to avoid duplicated entries in your $PATH, you can add this command in your ~/.profile file (that is the best location for environmental variables):

# remove duplicated paths in PATH variable
PATH="$(printf "%s" "$PATH" | awk -v RS=':' '!a[$1]++ {if (NR > 1) printf RS; printf $1}')"

This command use awk to identify every item in the $PATH variable (they are separated by means of the : character) and rebuild the $PATH variable avoiding re-adding duplicated folders. It also preserves the original sort of the folders inside the $PATH variable.

You can open ~/.profile with your preferred text editor. If you open it by terminal, you don't need sudo because it belongs to your user.