How to add a directory to your PATH

How to add a directory to your PATH

Julia Evans
@b0rk do you mind if I share my favourite $PATH deduplication trick?
@gnomon @b0rk is this the cursed one involving little-known readline shortcuts

@nev @b0rk SPOILERS NEV

(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)

Overwriting a environment variable in linux

I wish to add a path to the PATH variable. I have copied the $PATH variable into a file called t1 using echo echo $PATH > t1 I then edited t1 nano t1 I added my path /usr/local/batch: to the

Stack Overflow

@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

@gnomon thanks! what's a reason that you re-source you ~/.bashrc? (i guess when updating a config setting?)

@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 no that makes sense! it's a good reminder that I keep forgetting how history works in bash and that those details matter

@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)

How to add a directory to your PATH

How to add a directory to your PATH

Julia Evans
@b0rk I don't think "wishy-washy" is being entirely fair to yourself there, you're very wisely stepping wide of a LARGE kettle of fish there

@gnomon @b0rk

<-- restarts shell session again

@gnomon I think I have no words at that. But then I use an ancient C program to do more or less the same thing and more (it looks to see if the directories actually exist and as a side effect sees through symlinks to list only one copy of the underlying thing). Maybe I should write up the background of it for the blog someday.
@cks please, I'd love to read that!
@gnomon Good news: this is going to be *two* entries, because the background does not fit in a single sensible-sized entry. If you guessed that it involves the fun of ancient Unix days, you are correct.
@cks I love your posts, man. Thank you for writing them.