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




Leave a Reply

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

You are commenting using your 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