Skip to content

Instantly share code, notes, and snippets.

@thoolihan
Created February 12, 2024 14:57
Show Gist options
  • Save thoolihan/6b033aafe7c8507d61f7a5b6bd7ea1a4 to your computer and use it in GitHub Desktop.
Save thoolihan/6b033aafe7c8507d61f7a5b6bd7ea1a4 to your computer and use it in GitHub Desktop.
Check for duplicates in your $PATH variable
#!/usr/bin/env bash
RAW_PATH_TMP=`mktemp`
CLEAN_PATH_TMP=`mktemp`
echo $PATH | sed 's/:/\n/g' | sort > $RAW_PATH_TMP
cat $RAW_PATH_TMP | uniq > $CLEAN_PATH_TMP
declare -i LINES=$(( `diff $RAW_PATH_TMP $CLEAN_PATH_TMP | wc -l` / 2 ))
if [ $LINES != 0 ]
then
echo -e "The following $LINES paths are duplicated in your PATH variable:"
diff -U 0 $RAW_PATH_TMP $CLEAN_PATH_TMP | sed -n '3,$p' | awk 'NR %2 == 0'
else
echo "No duplicates found in path"
fi
rm $RAW_PATH_TMP $CLEAN_PATH_TMP
exit $LINES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment