2.6. Shell Start-up Scripts

Each time you log into a Unix machine or start a new shell (e.g. when you open a new terminal), the shell process runs one or more special scripts called start up scripts. Some common start up scripts:

Table 2.2. Shell Start Up Scripts

ScriptShells that use itExecuted by
/etc/profile, ~/.profileBourne shell familyLogin shells only
File named by $ENV (typically .shrc or .shinit)Bourne shell familyAll interactive shells (login and non-login)
~/.bashrcBourne-again shell onlyAll interactive shells (login and non-login)
~/.bash_profileBourne-again shell onlyLogin shells only
~/.kshrcKorn shellAll interactive shells (login and non-login)
/etc/csh.login, ~/.loginC shell familyLogin shells only
/etc/csh.cshrc, ~/.cshrcC shell familyAll shell processes
~/.tcshrcT-shellAll shell processes

Note

Non-interactive Bourne-shell family shell processes, such as those used to execute shell scripts, do not execute any start up scripts. Hence, Bourne shell family scripts are not affected by start up scripts.

In contrast, all C shell script processes execute ~/.cshrc if it exists. Hence, C shell family scripts are affected by ~/.cshrc. You can override this in C-shell scripts by invoking the shell with -f as follows:

#!/bin/csh -f
	

The man page for your shell has all the details about which start up scripts are invoked and when.

Start up scripts are used to configure your PATH and other environment variables, set your shell prompt and other shell features, create aliases, and anything else you want done when you start a new shell.

One of the most common alterations users make to their start up script is editing their PATH to include a directory containing their own programs and scripts. Typically, this directory is named ~/bin, but you can name it anything you want.

To set up your own ~/bin to store your own scripts and programs, do the following:

  1. shell-prompt: mkdir ~/bin
  2. Edit your start up script and add ~/bin to the PATH.

    If you're using Bourne-again shell, you can add ~/bin to your PATH for login shells only by adding the following to your .bashrc:

    PATH=${PATH}:${HOME}/bin
    export PATH
    	    

    If you're using T-shell, add the following to your .cshrc or .tcshrc:

    setenv PATH ${PATH}:~/bin
    	    

    If you are using a different shell, see the documentation for your shell to determine the correct start up script and command syntax.

    Caution

    Adding ~/bin before (left of) ${PATH} will cause your shell to look in ~/bin before looking in the standard directories such as /bin and /usr/bin. Hence, if a binary or script in ~/bin has the same name as another command, the one in ~/bin will override it. This is considered a security risk, since users could be tricked into running a Trojan-horse ls or other common command if care is not taken to protect ~/bin from modification.

    Hence, adding to the tail (right side) of PATH is usually recommended, especially for inexperienced users.

  3. Update the PATH in your current shell process by sourcing the start up script, or by logging out and logging back in.

There is no limit to what your start up scripts can do, so you can use your imagination freely and find ways to make your Unix shell environment easier and more powerful.

2.6.1. Self-test

  1. What is the purpose of a shell start-up script?
  2. What are the limitations on what a start-up script can do compared to a normal script?