How to correctly add a path to PATH?

I'm wondering where a new path has to be added to PATH environment variable. I know this is accomplished editing .bash_rc (for example), but it's not clear how to do this.
This way:
export PATH=~/opt/bin:$PATH
or this?
export PATH=$PATH:~/opt/bin
Question 2 (related). What's a workable way to append more paths on different lines? Initially I thought this could do the trick:
export PATH=$PATH:~/opt/bin
export PATH=$PATH:~/opt/node/bin
but it doesn't because the second assignment doesn't only append ~/opt/node/bin, but also the whole PATH previously assigned.
This is a possible workaround:
export PATH=$PATH:~/opt/bin:~/opt/node/bin
but for readability I'd prefer to have one assignment for one path.


Either way works, but they don't do the same thing: the elements of PATHare checked left to right. In your first example, executables in ~/opt/bin will have precedence over those installed, for example, in /usr/bin, which may or may not be what you want.
In particular, from a safety point of view, it is dangerous to add paths to the front, because if someone can gain write access to your ~/opt/bin, they can put, for example, a different ls in there, which you'd then probably use instead of /bin/ls without noticing. Now imagine the same for ssh or your browser or choice... (The same goes triply for putting . in your path.)


Post a Comment

Don't Forget to comment