(no, not this one)
edit: this was the cursed trick: https://mastodon.social/@gnomon/111206929341206118
@gnomon @b0rk slightly less cursed but still hacky way i found on stackoverflow (https://stackoverflow.com/q/53264235) and have been using ever since:
echo ${PATH} > t1
<editor> t1
export PATH=$(cat t1)
@gnomon yeah i'd be interested, i've never known how to do that really
i'd also be curious about how you use it
@b0rk so first the trick, then the reasons for it:
mapfile -t paths < <(<<< "$PATH" tr ':' $'\n' | awk '!seen[$0]++'); printf -v 'PATH' ':%s' "${paths[@]}"; PATH=${PATH:1}
The real trick is that the awk step deduplicates entries _without changing the order of the path elements_, which of course is crucial in this case. The printf/PATH=... step is just about reassembling the array of results back into a string with minimal opportunities for errors to creep in.
1/2
@b0rk the reason I use this is that my ~/.profile and ~/.bashrc files mix direct assignment to $PATH with appending/prepending values, in some cases because that's how external tools I call have set themselves up. This deduplication hack lets me insert synchronization points that ensure re-source'ing my ~/.bashrc is idempotent, instead of accumulating a longer and longer value which I usually fail to notice.
2/2
@b0rk exactly, yeah. I almost always re-source my bashrc instead of starting a new shell because I have come to consider the last ~100 or so entries in my shell command history part of my peripheral memory, and I hate losing that if I can avoid it. Poor reason, I know.
edit: ugggh, and I just realized upon re-reading it anew that this could have been a one-liner:
```
PATH=$(<<< $PATH awk 'BEGIN{RS=ORS=":"}; !seen[$0]++')
```
Ah well, the shorter version is even less legible.
@gnomon added a couple of more problems to the post based on this ("duplicate PATH entries making it harder to debug" and "losing your history after updating your PATH”) https://jvns.ca/blog/2025/02/13/how-to-add-a-directory-to-your-path/#problem-3-duplicate-path-entries-making-it-harder-to-debug
(though I was a bit wishy washy about how to accomplish the deduplication exactly)