linuxinstallationenvironment-variablesbash

How can I programmatically set a permanent environment variable in Linux?


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).


Solution

  • 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.