No more manually updating .bashrc to add something to your PATH

dangarbri

New Member
Joined
Apr 1, 2022
Messages
16
Reaction score
10
Credits
161
As a linux user, I find myself often needing to update my .bashrc file to add something to my path. Then I must perform the lengthy process of opening my .bashrc, going to the bottom, adding export PATH=... wait, I forgot to copy the full path of what I need to add, etc.

So I wrote a quick script to do it for me. If something more robust than this exists, please let me know!

Check out the git on github

Usage:
addtopath somefolder "Adds XYZ commands to path"

This will append the following text to ~/.bashrc preceded by a new line:
# Adds XYZ commands to path export PATH=/full/path/to/somefolder:$PATH
 


N

NorthWest

Guest
In the distros I use, by default, PATH is usually set in .profile for the user, and for the system in /etc/profile. My use doesn't require changes to the PATH much at all since /usr/local/bin and /home/<username>/bin are where all the executables end up living. In the past some applications lived in /opt with executables under that directory, but instead of changing the PATH it was simpler to create links from /usr/local/bin. If those apps were deleted, the PATH was unaffected. YMMV.
 
Last edited by a moderator:
OP
dangarbri

dangarbri

New Member
Joined
Apr 1, 2022
Messages
16
Reaction score
10
Credits
161
Yea, I've seen some things get installed to /home/<username>/.local/bin as well, but in my experience that is also not automatically added to PATH.
 

SlowCoder

Member
Joined
May 2, 2022
Messages
96
Reaction score
66
Credits
791
It is very rare (read "hardly ever") that I need to modify my $PATH. In my recent memory, I only need to add my Java JRE path for Java coding and execution. I'm curious what you're doing that you need to modify your path "often"?


What is your use case? I'm seeing what would be a "code smell", in this case you have a script that doesn't seriously improve the purpose for which it is used, but adds complexity and potential for errors. I can just use "export PATH=$PATH;<pathtoadd>" for a temporary add, or add directly to my .bashrc PATH for permanence.


Also, depending on your priority, you may want to consider switching your export so that your new path is appended to the existing $PATH, rather than the other way around.

Yea, I've seen some things get installed to /home/<username>/.local/bin as well, but in my experience that is also not automatically added to PATH.
This may depend on the distro. In Debian based distros, ~/bin is usually in the path, but the directory may or may not exist by default. All the user needs to do is create ~/bin and any added executables will be found.
 
OP
dangarbri

dangarbri

New Member
Joined
Apr 1, 2022
Messages
16
Reaction score
10
Credits
161
My use case is when downloading binaries or creating scripts that are meant to live in a specific repository.

I suppose I could soft link things to ~/bin, but it's gets complicated when a script or binary is looking for files in its own directory, in which case you would need to know everything it uses and add each one to ~/bin or /usr/lib, instead I prepend things to my path.

I prefer prepending to my path that way if I'm compiling and testing something that may already be installed, then I want the one I compile to be found first.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Staff online


Top