Setting Environment Variables in Linux

Reading Time: 4 minutes

Introduction

Setting up environment and system variables is always of a bit of a deal in Linux. Windows based system have it figured out via GUI in Control Panel. The variables can be set for specific to a user, as well as system-wide.

The same set of variables applies to the Linux as well. In this post, I’ll conclude all the things that I’ve encountered in my previous mistaken experiences and the information that I have derived over the internet.

In this post, I’ll demonstrate how you can define multiple environments namely Ant and Java paths, then we will append the PATH with those.

Additionally, you may find this post a bit comprehensive, and may like to get to the business immediately. I strongly suggest using .bashrc and User Profile for Login methods. On top of that, you may read the rest of the content later to be enlightened about the details.

Common Environment Variables

  • SHELL: This describes the shell that will be interpreting any commands you type in. In most cases, this will be bash by default, but other values can be set if you prefer other options.
  • TERM: This specifies the type of terminal to emulate when running the shell. Different hardware terminals can be emulated for different operating requirements. You usually won’t need to worry about this though.
  • USER: The current logged in user.
  • PWD: The current working directory.
  • PATH: A list of directories that the system will check when looking for commands. When a user types in a command, the system will check directories in this order for the executable.
  • LANG: The current language and localization settings, including character encoding.
  • HOME: The current user’s home directory

Reference[1]

To print the environment values that were set previously, in terminal you can use env or printenv commands

Environment and Shell Variables

Environmental variables are variables that are defined for the current shell and are inherited by any child shells or processes. Environmental variables are used to pass information into processes that are spawned from the shell.

Shell variables are variables that are contained exclusively within the shell in which they were set or defined. They are often used to keep track of ephemeral data, like the current working directory.

By convention, these types of variables are usually defined using all capital letters. This helps users distinguish environmental variables within other contexts.[1]

Specific-User Variable Definitions

The variables will be defined only for a specific user.

User Profile for Login

This applies to the users that have a login to the system. Open the your profile file using nano:

nano ~/.profile

add the below content

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
export ANT_HOME=/path/to/ant/dir
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

To activate the changes immediately either use source or .

source ~/.profile

Shell Session

This applies to the users that only have a SSH login to the system. Open the your bash profile file using nano:

nano ~/.bash_profile

add the below content

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
export ANT_HOME=/path/to/ant/dir
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

To activate the changes immediately either use source or .

source ~/.bash_profile

System-wide Variable Definitions

The variables defined this way will be visible not only for users, but anywhere in the whole system.

Using .bashrc

Fortunately, most Linux distributions configure the login configuration files to source the non-login configuration files. This means that you can define environmental variables that you want both inside and the non-login configuration files. They will then be read in both scenarios.

We will usually be setting user-specific environmental variables, and we usually will want our settings to be available in both login and non-login shells. This means that the place to define these variables is in the ~/.bashrc file.[1]

Open the file with nano

nano ~/.bashrc

add the below content

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
export ANT_HOME=/path/to/ant/dir
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

To activate the changes immediately either use source or .

source ~/.bash_profile

Using /etc/profile

/etc/profile contains Linux system wide environment and startup programs. It is used by all users with bash, ksh, sh shell. Usually used to set PATH variable, user limits, and other settings for user. It only runs for login shell. If you wanted to make large changes or application specific changes use /etc/profile.d/[2]

Open the file with nano

sudo nano /etc/profile

add the below content

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
export ANT_HOME=/path/to/ant/dir
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

To activate the changes immediately either use source or .

source /etc/profile

Using an Independent sh file in profiles

This sort of solution came up on my way while I was looking for an alternative. I fully grabbed it from the Ubuntu forum. I personally vote for this method

Files with the .sh extension in the /etc/profile.d directory get executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads. [3]

Use nano to create the “sh” file

sudo nano /etc/profile.d/java.sh

add the below content

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin

To activate the changes immediately either use source or .

source /etc/profile.d/java.sh

Using /etc/environment

This solution is supported in the Ubuntu forum[3] and I have used this way for a quite long time as well. However, in Debian I have seen some difficulties of it as well. While looking solutions out there, I found these really useful Linux Stack exchange posts that enlightened me[4][5]. Let’s look at the definition of this file

/etc/environment is a configuration file for pam_env, not a file read by a shell. The syntax is somewhat similar, but it is not the same. In particular, you can’t refer to existing variables: you’ve set your search path to contain $ORACLE_HOME/bin and $PATH, i.e. directories with a dollar sign in their name.

Open the file with nano

sudo nano /etc/environment

add the below content

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin

To activate the changes immediately either use source or .

source /etc/environment

References

  1. How To Read and Set Environmental and Shell Variables on a Linux VPS
  2. https://www.cyberciti.biz/faq/set-environment-variable-linux/
  3. System-wide environment variables
  4. Setting variables in /etc/environment not having an affect but setting them in command line is [duplicate]
  5. Why does environment variable persistence break the PATH var?
  6. How to permanently export a variable in Linux?