How to programmatically set a permanent environment variable in Linux?
LSB-compliant (see spec) practice is to create a shell script in /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 source
d.
Note that this is not enforced by bash
; rather, it's an agreement of sorts.
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?