How to add a directory to your PATH

How to add a directory to your PATH

Julia Evans

@b0rk I believe all of the bash/zsh examples should be using quoted strings on the right hand side of the assignment:

export PATH="/some/dir:$PATH"

This is not always necessary but it's much easier and safer to always do it than to remember when it isn't necessary.

Unfortunately it has the side effect of blocking expansion of ~, so those need to get replaced with $HOME:

export PATH="$HOME/.npm-global/bin:$PATH"

@zwol interesting, why? I have spaces in my PATH but it seems to work fine for me without quotes, what are you seeing?

@b0rk I'm being defensive, not against anything that might go wrong *here*, but against people picking up bad habits.

In bash and zsh (and the entire "Bourne shell" family) it *happens* to be safe to leave the double quotes off the right-hand side of an assignment, even if there is a variable in there that expands to something with spaces inside. Word splitting won't happen. But leaving the double quotes off *any* shell expression that contains variable expansions (or any other kind of expansion) is a bad habit to be in, because in *most* contexts word splitting *will* happen. It is better, in my opinion, not to try to remember when it won't, but instead to *always* double-quote unless you specifically *want* word splitting.

@zwol oh I see! I'm unlikely to tell people that they should quote $PATH in that case

(I'm sure that trying to prevent people from "picking up bad habits" is helpful sometimes but it feels kind of paternalistic to me and it's not really the kind of advice that I give)

@b0rk Hm. I don't want to be paternalistic. Maybe I shouldn't phrase this advice in terms of bad habits? In my head it's about suggesting that people can make their lives easier by not bothering to remember something. Do you think that's a less obnoxious way to put it?
@zwol I mean if that were something I did I might say something like "I like to quote $PATH even though it's not strictly necessary because it gets me in the habit of remembering to quote shell variables and it helps me avoid making quoting mistakes in my shell scripts”
@zwol (my approach to “not forgetting to quote variables in shell scripts" is to always automatically run every shell script I type through `shellcheck`, which is I guess a different way of achieving the same thing and works better for me personally)
@b0rk shellcheck is great but unfortunately it cannot be used on autoconf scripts (all the problems could be fixed but no one has the energy)
@zwol that that makes a lot of sense! honestly the reason I love hearing why people have specific preferences is that everyone's workflow/restrictions are different (like "I write autoconf scripts so I can't use shellcheck") and it's fun to hear how people end up in different places as a result