15

I am writing a little install script for some software. All it does is unpack a target tar file, and then I want to permanently set some environment variables - principally the location of the unpacked libraries and updating $PATH. Do I need to programmatically edit the .bashrc file, adding the appropriate entries to the end for example, or is there another way? What's standard practice?

The package includes a number of run scripts (20+) that all use these named environment variables, so I need to set them somehow (the variable names have been chosen such that a collision is extremely unlikely).

1
  • 1
    Why would you do this at all? If you're packaging a given piece of software, you can make your packaging wrap it in a shell wrapper that sets the needed variables only when that specific software is being run, and has no need to modify the larger system's behavior otherwise. Commented Dec 1, 2024 at 16:57

3 Answers 3

17

LSB-compliant (see the specification) practice is to create a shell script in the /etc/profile.d/ folder.

Name it after your application (and make sure that the name is unique), make sure that the name ends with .sh (you might want to add scripts for other shells as well) and export the variables you need in the script. All *.sh scripts from that directory are read at user login—the same time /etc/profile is sourced.

Note that this is not enforced by Bash; rather, it's an agreement of sorts.

Sign up to request clarification or add additional context in comments.

2 Comments

ok this looks like it might be the solution. presumably the installer will need to be run as root in order to write an executable script here.
Are this script read also by services? For example apache or tomcat?
3

Standard practice is to install into directories already in the path and in the standard library directory, so there is no need to update these variables.

Updating .bashrc is a bit failure-prone, among other things; what if a user uses a different file or shell?

3 Comments

+1 correct. Users would be very unhappy if you tried to edit their .bashrc for them. If you want to install into a non-standard directory - or don't think your user will have the permission to - let them specify --install-dir=mydir and tell them what they'd need to add to their environment. There is a good example at golang.org/doc/install.html
yes, i thought that editing .bashrc would be dubious for a number of reasons. The problem is the software contains a number of scripts (20+) that all use these named environment variables - so i need to set them somehow.
One way is to source them in a wrapper script that calls your scripts.
2

You can also generate and install a script that sets those variables. Users of your package then source that script or copy its contents to their own shell init file.

1 Comment

Or a wrapper script calls the var-setting script at a known name and place and then calls the initial executable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.