sections in this module City College of San Francisco - CS260A
Unix/Linux System Administration

Module: Users and Groups
module list

Startup Files (shell initialization files)

When you login to a Unix or linux system, a single command is run for you. Assuming that command is a shell, it executes a number of commands prior to the printing of the first prompt on the screen. These shell commands are kept in a sequence of 'shell initialization files' or startup files, which are sourced (read in and executed in the current shell using the dot operator: as a commands file, rather than in a child process as a shell script). We will cover the sequence of startup files sourced for the common POSIX-standard startup files bash and ksh. Some of these startup files are sourced for login shells only. Some are sourced for every shell (i.e., including sub-shells) To understand this, we must first discuss the different types of shells you can run. 

Types of shells

Each shell that runs is defined as interactive or non-interactive. An interactive shell repeatedly prompts the user and retrieves a command typed in at the command-line. It then executes the command. Any time you type a command at the shell prompt you are talking to an interactive shell.

The most obvious interactive shell that you run is your login shell. This shell requires the most initialization. Normally, a single login shell is run for each user when they log in. As indicated, login shells are by definition interactive. Login shells can usually be distinguished because the name of the shell command (as output by ps(1)) has a dash prefix. If you login to a system using ssh or using a virtual console (a dumb terminal interface) you should be running a login shell. If you login to a linux system using a GUI, you probably never see your login shell. It is run on your behalf by the desktop manager.

The next obvious shell type is a sub-shell. At the commandline you could run a subshell by typing bash at the $ prompt, resulting in running a second shell as the child of your login shell. Since the subshell prompts the user and retrieves a command typed in at the command-line, it is interactive. It is called an interactive non-login shell. If you login to a linux system using a GUI, the shell opened for you when you start a terminal application is an interactive non-login shell.

NOTE: At the time of this writing, the shell started for you at the GUI could either be a login or non-login shell depending on how you started it! If you right-click on the desktop, the shell is a login shell. If you run the Terminal application either through the menu or using a toolbar icon, a non-login shell is started!

The last type of shell you can run is used to run a shell script. Although this shell executes commands, it does not interact with you to retrieve them (although it could still read input from you using standard input). A shell that is running a shell script is a non-interactive non-login shell. Note that this only applies to shell scripts executed directly. If you start a sub-shell and feed it a script using input redirection it is considered an interactive shell.

These three different types of shells have different rules for the sourcing of startup files. These are the rules for the bash shell:


login shell interactive
  non-login shell
non-interactive
non-login shell
sourced at shell
 initialization
/etc/profile
then, the first file found
in this list:
~/.bash_profile
~/.bash_login
~/.profile  
~/.bashrc the file indicated by the
environment variable
$BASH_ENV
sourced at
shell exit
~/.bash_logout

This looks simple, but several of these files source other files, and sometimes they even source each other!  Let's deal with that later. First, let's discuss the sorts of things each file normally does:

The system profile file /etc/profile

The /etc/profile file is the only commands file that is guaranteed to be sourced at login by users of any Bourne-compatible shell and that is not under the control of the user. This file is used by the administrator to initialize accounts to default values and to do any bookkeeping required. Each bash user on the system runs these commands when they log in. After /etc/profile has been sourced you have:

On linux, /etc/profile does this by sourcing a host of other files from the directory /etc/profile.d. (Note: some of these settings may be initialized before /etc/profile has run, depending on the system.) 

The user profile file

The user profile file is run by login shells to do any initialization tasks that should be done once at login. For bash users this is typically named ~/.bash_profile, but if there is no ~/.bash_profile file, the old Unix-standard profile file ~/.profile will be sourced. (.bash_login was the name of .bash_profile used in earlier bash shells, and is retained for backwards compatibility) If the user wishes to set (or modify) environment variables, user limits, the umask, change terminal settings, or run any programs at login, this is where it is done. One environment variable that should be created here is the BASH_ENV variable, which is used by non-interactive shells to find the file they source, if any. On linux, the ~/.bash_profile also usually sources ~/.bashrc

The Korn and POSIX shells use the standard ~/.profile for their user profile file.

The user environment file ~/.bashrc

The bash environment file is used to initialize interactive shells, although this is configurable for login shells. Initializations that are local to the shell, such as local variables and aliases, should be done in the environment file.

Normally the user environment file sources a system environment file /etc/bashrc, if it exists, so that system-wide initialization can be done. Sourcing /etc/bashrc is done at the beginning of the ~/.bashrc file, before any user modifications are performed. Note that the /etc/bashrc file may set the default umask of the shell. If the user has a umask preference, then, it should be set in the environment file after /etc/bashrc is sourced.

Whether non-interactive shells read the environment file or not is determined by whether the environment variable BASH_ENV is set. If BASH_ENV is set and contains the path to a readable file, non-interactive shells source $BASH_ENV. Usually, BASH_ENV is initialized to ~/.bashrc in the ~/.bash_profile file.

Note that, although the path to the environment file is variable (using BASH_ENV) for non-interactive shells, it is fixed for interactive non-login shells. Those shells source only the file ~/.bashrc, if it exists.

POSIX- and Korn shells use the ENV environment variable to locate the environment file. All shells (and I believe this includes non-interactive shells) source the $ENV file when they begin. (Login shells source this file as soon as the variable ENV is defined in the .profile file.)

Installation of shell initialization files

When an account is created, the entire contents of the initialization file skeleton directory is copied to the new user's home directory. The path to this skeleton directory is indicated in /etc/default/useradd, but it is usually /etc/skel.

Modification of startup files

The user's shell initialization files are hers. Naive users don't know the files exist, as they are hidden. More sophisticated users will want to add their own customization, such as to customize the prompt or add aliases. These files must be modified carefully, as the introduction of an error can result in disabling the user's account. If this occurs, the administrator can replace the modified files with copies of the original versions from /etc/skel.


Prev This page was made entirely with free software on linux:  
the Mozilla Project
and Openoffice.org      
Next

Copyright 2012  Greg Boyd - All Rights Reserved.