I'm trying to figure out the best way to store sourced functions in Bash. I know that `.bashrc` is the usual go-to place, but I'm concerned that it might not scale well for sharing across users. Is there a more standardized location where I could place these functions so that they're easily accessible to others?
3 Answers
There's really no official standard for where to put these sourced functions in Bash, since it lacks a library or module ecosystem like other languages. You can use any directory that's in your `$PATH` for sourcing, which often means placing the function file next to the script that needs it. Just keep in mind, if it's not in your `$PATH`, you'll have to specify the full path to source it, which can lead to some messy script hacks.
Some folks suggest using `/etc/profile.d` as a place to keep shared scripts, since scripts placed there are automatically sourced for all users. Alternatively, if you want something accessible by everyone, consider putting your files in `/usr/local/bin`.
Good question! While `/usr/local/bin` is typically for executable files, you can still use it for scripts that you source elsewhere. Just remember, if you want it to be sourced by multiple users, you need to make sure the script itself is set up correctly.
If you're considering using `.bashrc`, make sure you clarify whether you're referring to the personal version or the system-wide one at `/etc/bash.bashrc`. But honestly, if you want this to scale well, you might look into using directories like `/opt/local/functions`. I personally name my function files with a `.func` extension and source them from my `.bashrc` using a loop to cover all my setups.
But isn't `/usr/local/bin` meant for binaries? How does that work for a sourced file?