5

I've written a bash function called up which I share via github. Now I'm struggling with installation instructions. I know of two options to 'install' bash functions:

  1. put them into .bashrc
  2. put them into a file called .bash_functions and source that file into .bashrc

So, my idea would be to suggest downloading the function and modify the .bashrc file accordingly.

I investigated how similar projects implement this and some of them even manipulate the .bashrc file on installation (i.e. they put the function in there). It seems pretty common to do this but I feel that manipulating the .bashrc automatically is a bad idea.

So, what's the preferred way? Is there some consensus about this?

  • This is a text file, so as long as you just happen code to the end of that file and you know what your doing there's no real risk. I'm not aware of any binary meent to edit .bashrc . – Kiwy Nov 29 '13 at 10:18
  • If you have an installer, then make it optional: In a separate file. Also make it useful for you, you maybe the only person to use it. However if you like it, then others may also. – ctrl-alt-delor Nov 29 '13 at 10:31
  • 1
    Is it for system-wide installation, so that every user would have that extra function automatically available the next time they start a bash shell? Or is it for one user to install it just for himself? Or to you want to install it as a package and have an interactive tool to be called by users to customise their own interactive shell? Is it limited to bash? Are there still people using bash interactively nowadays? – Stéphane Chazelas Nov 29 '13 at 12:19
  • 2
    NEVER EVER touch my .bashrc! Just add instructions like: to make it available source yourbashrc, and people can decide for themselve what they want. – Bernhard Nov 29 '13 at 13:27
  • 1
    I once saw an installation instruction which overwrites the .bashrc. Glad I checked installation script :) – Bernhard Nov 29 '13 at 13:29
4

There are 3 ways to distribute:

  1. add lines to $HOME/.bashrc
  2. a file that is sourced by .bashrc
  3. a file that can be included into the directory /etc/profile.d

I would say it depends on the number of functions and their lengths. If it's 2 or more functions, and they are somewhat lengthy I would almost exclusively distribute them as a single file (or files) that gets sourced via either the user's $HOME/.bashrc file or incorporated into /etc/profile.d, via a script you provide them.

If it's a single function or perhaps 2, and they're quite short, then I would distribute them strictly as a .txt file or simply post them on github as code that is meant to be copy/pasted into your existing environment, i.e. included into $HOME/.bashrc or a preexisting file under /etc/profile.d, ultimately leaving it to the user where exactly.

NOTE: /etc/profile.d contains files for multiple shells (Csh, Bash, Zsh, etc.). Any files included here will be used by these other shells as well. The naming of files here is what controls which shells will utilize them. A .csh will provide for Csh/Tcsh, a .sh for Bash, Zsh, etc.

On the proper use of /etc/profile.d

If you're curious, files added to /etc/profile.d should contain commands that ought to only run once, at the beginning of login. (This includes graphical logins, as they start with a login shell, too.) If a shell is interactive, the user running it is probably logged on, and so it probably has an ancestor (that started it, or started what started it, or started that, etc.) that was a login shell.

See this excellent answer on AskUbuntu titled: Why is /etc/profile not invoked for non-login shells?, which details the differences between a interactive vs. login shell and their implications.

  • 2
    Profile is for session configuration (for login shells), it's not for shell customisation. (it's also not specific to bash). – Stéphane Chazelas Nov 29 '13 at 12:11
  • @StephaneChazelas - LMK if I should clarify your point further. I've included additional info on the use of "profile". – slm Nov 29 '13 at 13:53
  • 1
    Most systems have a /etc/bash.bashrc for system-wide customisation of bash interactive sessions. See also /etc/skel for configuration for future users. – Stéphane Chazelas Nov 29 '13 at 14:00
  • @StephaneChazelas - yes none of the Redhat distros I use have that, /etc/bash.bashrc. Also if you look in that file on RH distros it includes the /etc/profile.d, so it is effectively sourced for each interactive login, kind of negating what you're saying about it being only for login shells, no? – slm Nov 29 '13 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.