Skip to content

Instantly share code, notes, and snippets.

@anddam
Created July 12, 2021 13:08
Show Gist options
  • Save anddam/88a37a69722446e1440d129df7ea4ceb to your computer and use it in GitHub Desktop.
Save anddam/88a37a69722446e1440d129df7ea4ceb to your computer and use it in GitHub Desktop.
#!/bin/sh --
# example of .path_dirs file:
# /home/anddam/bin
# /home/anddam/.local/bin
{
# This 'config' should probably be found more nicely than this.
cat "${1:-$HOME/.path_dirs}"
IFS=:
# Wordsplitting is purposefully used here over a colon. PATH is
# guaranteed to be separated by colons so this is reliable.
# shellcheck disable=SC2086
printf '%s\n' $PATH
} | {
while read -r dir; do
# printf "dir=%s\n" $dir
[ -d "$dir" ] && set -- "$@" "$dir"
done
# printf 'arg=%s\n' "$arg"
for arg; do
# printf 'arg=%s\n' "$arg"
case :$tmp: in
*:$arg:*) ;;
*) path=${path:+$path:}$arg
esac
tmp=${tmp:+$tmp:}$arg
done
printf '%s\n' "$path"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment