-->

Locating System Environment Variables On Linux

The Linux system uses environment variables for many purposes. You know now how to modify system environment variables and create your own variables. The trick is in how these environment variables are made persistent.

Locating System Environment Variables On Linux
Locating System Environment Variables On Linux

When you start a bash shell by logging in to the Linux system, by default bash checks several files for commands. These files are called startup files or environment files. The startup files that bash processes depend on the method you use to start the bash shell. You can start a bash shell in three ways:

  • As a default login shell at login time
  • As an interactive shell that is started by spawning a subshell
  • As a non-interactive shell to run a script

The following sections describe the startup files the bash shell executes in each of these startup methods.

Understanding the login shell process

When you log in to the Linux system, the bash shell starts as a login shell. The login shell typically looks for five different startup files to process commands from:


  • /etc/profile
  • $HOME/.bash_profile
  • $HOME/.bashrc
  • $HOME/.bash_login
  • $HOME/.profile


The /etc/profile file is the main default startup file for the bash shell on the system. All users on the system execute this startup file when they log in.

Note
Be aware that some Linux distributions use Pluggable Authentication Modules (PAM). In this case, before the bash shell is started, PAM files are processed, including ones that may contain environment variables. PAM file examples include the /etc/environment file and the $HOME/.pam_environment file. Find more information about PAM at http://linux-pam.org.

The other four startup files are specific for each user and can be customized for an individual user’s requirements. Let’s look closer at these files.

Viewing the /etc/profile file

The /etc/profile file is the main default startup file for the bash shell. Whenever you log in to the Linux system, bash executes the commands in the /etc/profile startup file first.

Different Linux distributions place different commands in this file. On this Ubuntu Linux system, the file looks like this:

  1. $ cat /etc/profile
  2. # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
  3. # and Bourne compatible shells (bash(1), ksh(1), ash(1), …).
  4. if [ ”$PS1” ]; then
  5.     if [ ”$BASH” ] && [ ”$BASH” != ”/bin/sh” ]; then
  6.         # The file bash.bashrc already sets the default PS1.
  7.         # PS1=’\h:\w\$ ‘
  8.         if [ -f /etc/bash.bashrc ]; then
  9.             . /etc/bash.bashrc
  10.         fi
  11.     else
  12.         if [ ”`id -u`” -eq 0 ]; then
  13.             PS1=’# ‘
  14.         else
  15.             PS1=’$ ‘
  16.         fi
  17.     fi
  18. fi
  19. # The default umask is now handled by pam_umask.
  20. # See pam_umask(8) and /etc/login.defs.
  21.     if [ -d /etc/profile.d ]; then
  22.         for i in /etc/profile.d/*.sh; do
  23.             if [ -r $i ]; then
  24.                 . $i
  25.             fi
  26.         done
  27.         unset i
  28.     fi
  29.     $

Most of the commands and syntax you see in this file are covered in and beyond. Each distribution’s /etc/profile file has different settings and commands. For example, notice that a file is mentioned in this Ubuntu distribution’s /etc/profile file above, called /etc/bash.bashrc. It contains system environment variables.

However, in this CentOS distribution’s /etc/profile file listed below, no /etc/bash.bashrc file is called. Also note that it sets and exports some system environment variables within itself:

  1. $ cat /etc/profile
  2. # /etc/profile
  3. # System wide environment and startup programs, for login setup
  4. # Functions and aliases go in /etc/bashrc
  5. # It’s NOT a good idea to change this file unless you know what you
  6. # are doing. It’s much better to create a custom.sh shell script in
  7. # /etc/profile.d/ to make custom changes to your environment, to
  8. # prevent the need for merging in future updates.
  9. pathmunge () {
  10.     case “:${PATH}:” in
  11.         *:”$1”:*)
  12.             ;;
  13.         *)
  14.             if[ “$2” = “after” ] ; then
  15.                 PATH=$PATH:$1
  16.             else
  17.                 PATH=$1:$PATH
  18.             fi
  19.         esac
  20. }
  21. if [-x/usr/bin/id ]; then
  22.     if [ -z “$EUID” ]; then
  23.         # ksh workaround
  24.         EUID=`id -u`
  25.         UID=`id -ru`
  26.     fi
  27.     USER=”`id -un`”
  28.     LOGNAME=$USER
  29.     MAIL=”/var/spool/mail/$USER”
  30. fi
  31. # Path manipulation
  32. if [ “$EUID” = “0” ]; then
  33.     pathmunge /sbin
  34.     pathmunge /usr/sbin
  35.     pathmunge /usr/local/sbin
  36. else
  37.     pathmunge /usr/local/sbin after
  38.     pathmunge /usr/sbin after
  39.     pathmunge /sbin after
  40. fi
  41. HOSTNAME=`/bin/hostname 2>/dev/null`
  42. HISTSIZE=1000
  43. if [ “$HISTCONTROL” = “ignorespace” ] ; then
  44.     export HISTCONTROL=ignoreboth
  45. else
  46.     export HISTCONTROL=ignoredups
  47. fi
  48. export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
  49. # By default, we want umask to get set. This sets it for login shell
  50. # Current threshold for system reserved uid/gids is 200
  51. # You could check uidgid reservation validity in
  52. # /usr/share/doc/setup-*/uidgid file
  53. if [ $UID -gt 199 ] && [ “`id -gn`” = “`id -un`” ]; then
  54.     umask 002
  55. else
  56.     umask 022
  57. fi
  58. for i in /etc/profile.d/*.sh ; do
  59.     if [ -r “$i” ]; then
  60.         if [ “${-#*i}” != “$-” ]; then
  61.             . “$i”
  62.         else
  63.             . “$i” >/dev/null 2>&1
  64.         fi
  65.     fi
  66. done
  67. unset i
  68. unset -f pathmunge
  69. $

Both distributions’ /etc/profile files use a certain feature. It is a for statement that iterates through any files located in the /etc/profile.d directory. (for statements are discussed in detail in Chapter 13.) This provides a place for the Linux system to place application-specific startup files that is executed by the shell when you log in. On this Ubuntu Linux system, the following files are in the profile.d directory:

$ ls -l /etc/profile.d
total 12
-rw-r—r— 1 root root 40 Apr 15 06:26 appmenu-qt5.sh
-rw-r—r— 1 root root 663 Apr 7 10:10 bash_completion.sh
-rw-r—r— 1 root root 1947 Nov 22 2013 vte.sh
$

You can see that this CentOs system has quite a few more files in /etc/profile.d:

$ ls -l /etc/profile.d
total 80
-rw-r—r—. 1 root root 1127 Mar 5 07:17 colorls.csh
-rw-r—r—. 1 root root 1143 Mar 5 07:17 colorls.sh
-rw-r—r—. 1 root root 92 Nov 22 2013 cvs.csh
-rw-r—r—. 1 root root 78 Nov 22 2013 cvs.sh
-rw-r—r—. 1 root root 192 Feb 24 09:24 glib2.csh
-rw-r—r—. 1 root root 192 Feb 24 09:24 glib2.sh
-rw-r—r—. 1 root root 58 Nov 22 2013 gnome-ssh-askpass.csh
-rw-r—r—. 1 root root 70 Nov 22 2013 gnome-ssh-askpass.sh
-rwxr-xr-x. 1 root root 373 Sep 23 2009 kde.csh
-rwxr-xr-x. 1 root root 288 Sep 23 2009 kde.sh
-rw-r—r—. 1 root root 1741 Feb 20 05:44 lang.csh
-rw-r—r—. 1 root root 2706 Feb 20 05:44 lang.sh
-rw-r—r—. 1 root root 122 Feb 7 2007 less.csh
-rw-r—r—. 1 root root 108 Feb 7 2007 less.sh
-rw-r—r—. 1 root root 976 Sep 23 2011 qt.csh
-rw-r—r—. 1 root root 912 Sep 23 2011 qt.sh
-rw-r—r—. 1 root root 2142 Mar 13 15:37 udisks-bash-completion.sh
-rw-r—r—. 1 root root 97 Apr 5 2012 vim.csh
-rw-r—r—. 1 root root 269 Apr 5 2012 vim.sh
-rw-r—r—. 1 root root 169 May 20 2009 which2.sh
$

Notice that several files are related to specific applications on the system. Most applications create two startup files one for the bash shell (using the .sh extension) and one for the c shell (using the .csh extension).

The lang.csh and lang.sh files attempt to determine the default language character set used on the system and set the LANG environment variable appropriately.

Viewing the $HOME startup files

The remaining startup files are all used for the same function — to provide a user-specific startup file for defining user-specific environment variables. Most Linux distributions use only one or two of these four startup files:

$HOME/.bash_profile
$HOME/.bashrc
$HOME/.bash_login
$HOME/.profile

Notice that all four files start with a dot, making them hidden files (they don’t appear in a normal ls command listing). Because they are in the user’s HOME directory, each user can edit the files and add his or her own environment variables that are active for every bash shell session they start.

Note
Environment files are one area where Linux distributions vary greatly. Not every $HOME file listed in this section exists for every user. For example, some users may have only the $HOME/.bash_profile file. This is normal.

The first file found in the following ordered list is run, and the rest are ignored:

$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile

Notice that $HOME/.bashrc is not in this list. This is because it is typically run from one of the other files.

Tip
Remember that $HOME represents a user’s home directory. Also, the tilde (∼) is used to represent a user’s home directory.

This CentOS Linux system contains the following .bash_profile file:

$ cat $HOME/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
$

The .bash_profile startup file first checks to see if the startup file, .bashrc, is present in the HOME directory. If it’s there, the startup file executes the commands in it.

0 Response to "Locating System Environment Variables On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel