Set environment variable permanently in Linux

To set the environment variable in Linux permanently, you can insert the export command into startup file.

Example of command to set environment variable:
export JAVA_HOME=/usr/local/java/jre1.8

There are several startup files in Linux environment.

To set environment variable for a particular user, you can use ~/bashrc, ~/bash_profile or ~/bash_login.

If you plan to set system wide environment variable, you can use /etc/profile, /etc/bashrc, etc/profile or create a custom .sh file in profile.d folder.

Here the execution sequence of the startup files:-

Interative login shell (e.g login remotely via ssh)

  1. /etc/profile
    1.1 all the .sh files in /etc/profile.d (*if any)
  2. ~/.bash_profile or ~/.bash_login or ~/.profile (*executes only the first of those files found)

Interactive non-login shell (e.g Gnome terminal)

  1. /etc/.bashrc
  2. ~/.bashrc

Example:

Reference: https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s