Skip to content

Commit 1755f73

Browse files
committed
Add PATH management util fn that will always emit
a well-formed and cleanly deduplicated PATH string. This lets us set PATH declaratively, like this: export PATH="$(make_clean_PATH " ${HOME}/.local/bin ${HOME}/bin ${PATH} /usr/lib/postgresql/10/bin")" The util is also idempotent when called repeatedly with no argument, or with the same argument.
1 parent 922f86a commit 1755f73

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

scripting-utils.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
#!/usr/bin/env bash
22

33
#
4+
# PATH MANAGEMENT
5+
#
6+
7+
make_clean_PATH() {
8+
local my_path=${@:-"${PATH}"}
9+
10+
printf "%s\n" ${my_path} |
11+
# un-form PATH-like strings
12+
tr ':' '\n' |
13+
# De-duplicate. # ref: https://stackoverflow.com/a/20639730
14+
# Or use awk '!x[$0]++', as explained here https://stackoverflow.com/a/11532197
15+
cat -n | sort -uk2 | sort -nk1 | cut -f2- |
16+
# re-form lines into single-colon-separated PATH-like string
17+
tr '\n' ':' | tr -s ':' |
18+
# ensure no trailing colons
19+
sed 's/:$//g'
20+
}
21+
22+
423
# DEPENDENCY CHECKS
524
#
625

0 commit comments

Comments
 (0)