>

There are several configuraiton files in linux that can have environment variables set, we need a better understanding of different config files and set environment variables properly in them.

Use env or set command to list all environment variables that are related to the current $SHELL.

Common environment variables that are in most Linux distributions:
PAHT, HOME, PWD, USER, UID, SHELL, BASH, BASH_VERSION

Global Settings

Environment variables in below system files will have effects to all users

  • /etc/bash.bashrc in CentOS this could be /etc/bashrc, will be executed everytime a shell is opened.
  • /etc/environment variable set in this file will have system wide effect, will be loaded before /etc/profile. We can only set variables in this file, by which means we can not execute any command in this file. We can use source /etc/environment to make it take effect to current shell immediately, however, we have to restart system so that it will take effect to all shells.
  • /etc/profile this file will source /etc/bash.bashrc and executes all the shell scripts in /etc/profile.d. This file will be loaded only one time when system starts up

Contents in /etc/profile

$ cat /etc/profile
if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

If we want to set a system wide variable, don’t add it in /etc/profile, add a customed script *.sh in /etc/profile.d. Especially, never add or modify a system wide variabe in /etc/bash.bashrc because this file will be loaded before any other config files when the system starts up, if we add a variable like ‘export JAVA_HOME=…’ in it, this may cause the normal system variable is overriden and falsely read, in worst case, if the PATH is not read correctly, all commands will not be able to executed as it will prompt the message saying “command is not found”.

Note that /etc/profile is loaded later than /etc/environment, we should put user-defined variable in customed script in /etc/profile.d, to achieve the least effect to system startup.

$ cat /etc/bash.bashrc

# System-wide .bashrc file for interactive bash(1) shells.
# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.

We set environment variable according to our requirement. For example, if we want all users in this system to be able to access JAVA, we can set JAVA_HOME in /etc/enviroment.

User Settings

Variables in below user-scope files have effect only on current user.

  • ~/.bashrc
  • ~/.bash_profile this file will automaticall source ~/.bashrc
  • /.profile this file is not read by bash if `/.bash_profileor~/.bash_login` exists

Contents in ~/.bash_profile

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

export

export is only effective to the current shell, if we open another shell, variables exported in current shell will not be set.