31 Mart 2014 Pazartesi

Setting Environment Variables On Linux

You can set environment variables in Linux by export command. 

In this post, I will set JAVA_HOME and M2_HOME. 

I am using Centos 6.5. Below explanations should also work for Red Hat implementations. In other Linux implementations, it could differ.


1. export Command

1.1. Define variable

This defines JAVA_HOME variable:
> export JAVA_HOME=/usr/lib/jvm/jdk-1.7.0

You can see your new variable by typing:

> echo $JAVA_HOME

When assigning a value to a variable, do not use $ (dollar) sign.

When assigning a value to a variable, do not leave spaces between = (equals) sign.


1.2. You can append to PATH variable

To use your executables without typing the full path, you can append your environment variables to PATH variable:
export JAVA_HOME=/usr/lib/jvm/jdk-1.7.0
export PATH=$PATH:$JAVA_HOME/bin


1.3. You can assign/append multiple variables:

> export JAVA_HOME=/usr/lib/jvm/jdk-1.7.0
> export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.4
> export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin

export command can be run from command line or can be put inside a shell script.



2. Running from Command Line

If you define new environment variables from command line, these variables will only be available to current shell session and its sub sessions. 

This implies that:

1. If you define a variable in a shell and exit later on ( for example by typing exit or closing the shell window), defined variables will be lost.
2. If you open two shell windows and define a variable in first shell, the second shell will not be aware of new variable. It will only be available to first session.
3. If you define a variable in a shell window and open a child shell (for example by typing bash), this new variable will also be available in child shell session.

So we must put our environment variables in specific files to make them permanent.



3. Permanent Environment Variables

To make an environment variable permanent, export commands must be placed in specific files.
When a shell is started, it reads a collection of startup files to help create an environment. By assigning environment variables in these files, every shell will read these variables.


3.1. System-wide Environment Variables

The recommended way is creating a shell script under /etc/profile.d and putting your export commands inside this script. 

Follow these steps:

    1. Create a shell script named environmentvariables.sh under /etc/profile.d

    2. Write:

export JAVA_HOME=/usr/lib/jvm/jdk-1.7.0
export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.4
export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin

    3. After creating this file, re-login to your account for a clear environment creation.This change will be system-wide.


environmentvariables.sh will be read by /etc/profile when a new login shell is started. You can get a new login shell by re-logging in your computer or refreshing your ssh connection.



3.1.1. Other Files

Another file that can be used to put environment variables is /etc/bashrc

In my tests, this file is read whenever a new shell is started. However, since there are condition checks in this file, it will be effective when a non-login interactive shell is started. Opening a new terminal or typing "bash" will start a new non-login interactive shell.


This file also reads scripts under /etc/profile.d, if the shell is non-login interactive shell. This also suggests that rather than modifying /etc/bashrc, you should put your export commands into scripts under /etc/profile.d. 



3.2. User Specific Environment Variables

The recommended way is assigning environment variables inside the ~/.bash_profile file which is user specific version of /etc/profile. Changes to this file will be read, when a new login shell is started for a specific user.

After modifying this file, re-login to your account or refresh your shh connection.



3.2.1. Other Files

~/.bashrc is often suggested to put environment variables for user specific behaviour.

For detailed discussion of shell types and their configuration files, you can read my next post.


Hiç yorum yok:

Yorum Gönder