TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to add a directory to your PATH

38 pointsby alexchamberlain3 months ago

12 comments

ljm3 months ago
The author&#x27;s last post was about terminal frustrations[0]<p>This is easily one of the most annoying ones, especially when shells will have several different locations to read config from depending on how you start the shell, or what a desktop app will do to try and pull the config in (`exec-path-from-shell` in emacs, for example).<p>And you can&#x27;t really, say, put it in both your `.zprofile` and `.zshrc` or `.bash_profile`, `.profile`, and `.bashrc`, because then it&#x27;ll get executed more than once and you&#x27;d need to maintain some kind of state to prevent that.<p>[0]<a href="https:&#x2F;&#x2F;jvns.ca&#x2F;blog&#x2F;2025&#x2F;02&#x2F;05&#x2F;some-terminal-frustrations&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jvns.ca&#x2F;blog&#x2F;2025&#x2F;02&#x2F;05&#x2F;some-terminal-frustrations&#x2F;</a>
评论 #43195354 未加载
评论 #43195179 未加载
evntdrvn3 months ago
I&#x27;ve worked on maintaining internal dev tooling for some small companies for a while now, and it&#x27;s a real PITA to write a robust installation script for bootstrapping a new laptop running an arbitrary shell on linux or macOS into a working environment. Way more work than it feels like it should be.<p>At this point I&#x27;ve pretty much given in and decided that a containerized dev environment is probably the better solution, but on principle it feels so unsatisfying to have to resort to this :(<p>(I know someone is going to mention Nix&#x2F;Guix ;) but that feels like a giant rabbit hole)
t435623 months ago
FWIW in bash I have 2 functions:<p><pre><code> path_add() { export PATH=$PATH:$(string_join &#x27;:&#x27; $@) } path_prepend() { PATH=$(string_join &#x27;:&#x27; &quot;$@&quot;):$PATH export PATH } </code></pre> These can join an arbitrary list of paths to PATH. e.g.<p><pre><code> path_add &#x2F;usr&#x2F;bin &#x2F;usr&#x2F;local&#x2F;bin ~&#x2F;bin </code></pre> They depend on another one:<p><pre><code> string_join() { local join=$1; shift local result=$1; shift for p in &quot;$@&quot;; do result=&quot;${result}${join}${p}&quot; done echo -n &quot;$result&quot; set +x } </code></pre> I also have ones for adding and prepending to LD_LIBRARY_PATH
评论 #43195903 未加载
评论 #43195432 未加载
Joker_vD3 months ago
As for path duplication, I personally have<p><pre><code> printf &#x27;%s\n&#x27; &quot;$PATH&quot; | grep --color=auto -E -e &#x27;(^|:)&#x27;&quot;$( printf &#x27;%s\n&#x27; &quot;$1&quot; | sed &#x27;s&#x2F;[][\.|$(){}?+*^]&#x2F;\\&amp;&#x2F;g&#x27; )&quot;&#x27;($|:)&#x27; &gt; &#x2F;dev&#x2F;null 2&gt;&amp;1 if [ &quot;$?&quot; = 1 ]; then PATH=&quot;$1:$PATH&quot; ; export PATH ; fi </code></pre> in my version of pathadd().
jmholla3 months ago
&gt; Configure your shell to continuously save your history instead of only saving the history when the shell exits. (How to do this depends on whether you’re using bash or zsh, the history options in zsh are a bit complicated and I’m not exactly sure what the best way is)<p>I attempted this in bash like so:<p>``` PROMPT_COMMAND=&quot;history -a; $PROMPT_COMMAND&quot; ```<p>But, that meant every shell was sharing the same history. Pressing up would go to the last command run anywhere, not just my current shell.<p>Sadly, I wasn&#x27;t even trying to solve the problem Julia is talking about here. I just wanted to make sure my history was saved when I shutdown. Still haven&#x27;t found a great solution to that. My attempts as using traps and signals caused weird issues.
评论 #43201346 未加载
duped3 months ago
In my (fever) dreams, there&#x27;s a directory at `&#x2F;var&#x2F;share&#x2F;env` with the rule that regular files are &lt;file name&gt;=&lt;file contents&gt; and all other file types are ignored. The `env` program can slurp all the files to create the default environment before applying whatever customization are in dotfiles for a shell.<p>Want to add to path? `echo &#x27;:&lt;directory&gt;&#x27; &gt;&gt; &#x2F;var&#x2F;share&#x2F;env&#x2F;PATH` (or `env --add PATH :&lt;directory&gt;`, or something like that).
评论 #43198537 未加载
评论 #43195689 未加载
MisterTea3 months ago
All this path stuff would go away if you have a proper vfs where you bind you other bins over &#x2F;bin allowing $path to simply read &quot;&#x2F;bin .&quot; and be done with it.
评论 #43195369 未加载
t435623 months ago
.bashrc gets run every time a shell starts<p>I&#x27;m not sure I&#x27;d stick a path setting in .bashrc - just because it would make it very difficult to ever override that PATH setting elsewhere.<p>You might want that but it might also mess up some other program&#x27;s attempt to set the PATH and then e.g. run a shell command.<p>I usually use .bash_profile or .profile more for this sort of thing and then I have to tell my terminal program to run bash as a login shell and that gives me what I mostly expect.
评论 #43195807 未加载
评论 #43199490 未加载
mathfailure3 months ago
I love this blog even though I usually get nothing from the articles that I didn&#x27;t know before. Still read it and still love it.
bandrami3 months ago
But not if the shell is running as an inferior process in an emacs buffer, in which case you have to do yet another thing.
评论 #43199786 未加载
评论 #43195445 未加载
fuzzfactor3 months ago
Here&#x27;s a possible idea for an open-source project if someone is qualified.<p>Make it as easy as it is in Windows.
评论 #43195309 未加载
评论 #43195087 未加载
评论 #43194916 未加载
评论 #43194958 未加载
unchar13 months ago
&gt; fish instructions:<p>&gt; set PATH $PATH ~&#x2F;.npm-global&#x2F;bin<p>Fish has some nice utilities for these type of set calls<p>set --append PATH ~&#x2F;.npm-global&#x2F;bin