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.
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:
The following sections describe the startup files the bash shell executes in each of these startup methods.
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.
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.
Different Linux distributions place different commands in this file. On this Ubuntu Linux system, the file looks like this:
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:
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.
$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.
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.
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.
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:
- $ cat /etc/profile
- # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
- # and Bourne compatible shells (bash(1), ksh(1), ash(1), …).
- if [ ”$PS1” ]; then
- if [ ”$BASH” ] && [ ”$BASH” != ”/bin/sh” ]; then
- # The file bash.bashrc already sets the default PS1.
- # PS1=’\h:\w\$ ‘
- if [ -f /etc/bash.bashrc ]; then
- . /etc/bash.bashrc
- fi
- else
- if [ ”`id -u`” -eq 0 ]; then
- PS1=’# ‘
- else
- PS1=’$ ‘
- fi
- fi
- fi
- # The default umask is now handled by pam_umask.
- # See pam_umask(8) and /etc/login.defs.
- if [ -d /etc/profile.d ]; then
- for i in /etc/profile.d/*.sh; do
- if [ -r $i ]; then
- . $i
- fi
- done
- unset i
- fi
- $
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:
- $ cat /etc/profile
- # /etc/profile
- # System wide environment and startup programs, for login setup
- # Functions and aliases go in /etc/bashrc
- # It’s NOT a good idea to change this file unless you know what you
- # are doing. It’s much better to create a custom.sh shell script in
- # /etc/profile.d/ to make custom changes to your environment, to
- # prevent the need for merging in future updates.
- pathmunge () {
- case “:${PATH}:” in
- *:”$1”:*)
- ;;
- *)
- if[ “$2” = “after” ] ; then
- PATH=$PATH:$1
- else
- PATH=$1:$PATH
- fi
- esac
- }
- if [-x/usr/bin/id ]; then
- if [ -z “$EUID” ]; then
- # ksh workaround
- EUID=`id -u`
- UID=`id -ru`
- fi
- USER=”`id -un`”
- LOGNAME=$USER
- MAIL=”/var/spool/mail/$USER”
- fi
- # Path manipulation
- if [ “$EUID” = “0” ]; then
- pathmunge /sbin
- pathmunge /usr/sbin
- pathmunge /usr/local/sbin
- else
- pathmunge /usr/local/sbin after
- pathmunge /usr/sbin after
- pathmunge /sbin after
- fi
- HOSTNAME=`/bin/hostname 2>/dev/null`
- HISTSIZE=1000
- if [ “$HISTCONTROL” = “ignorespace” ] ; then
- export HISTCONTROL=ignoreboth
- else
- export HISTCONTROL=ignoredups
- fi
- export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
- # By default, we want umask to get set. This sets it for login shell
- # Current threshold for system reserved uid/gids is 200
- # You could check uidgid reservation validity in
- # /usr/share/doc/setup-*/uidgid file
- if [ $UID -gt 199 ] && [ “`id -gn`” = “`id -un`” ]; then
- umask 002
- else
- umask 022
- fi
- for i in /etc/profile.d/*.sh ; do
- if [ -r “$i” ]; then
- if [ “${-#*i}” != “$-” ]; then
- . “$i”
- else
- . “$i” >/dev/null 2>&1
- fi
- fi
- done
- unset i
- unset -f pathmunge
- $
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