Shell Start-up Scripts

When you start a new shell process (e.g. log in via ssh or open a new terminal window), the shell process may source one or more special scripts called start up scripts.

Which scripts are sourced depends on how the shell is started. Non-interactive Bourne shell family processes, such as those used to execute shell scripts, do not source any start up scripts by default.

In contrast, C shell scripts by default source ~/.cshrc if it exists. You can override this in C-shell scripts by invoking the shell with -f as follows:

#!/bin/csh -ef
        

Shell processes considered login shells will source additional scripts that non-login shells do not. For example, a shell process started remotely via ssh is a login shell. A shell process started when opening a new terminal window in a Unix GUI is not a login shell.

The man page for your shell has all the details about which start up scripts are sourced and when. Table 4.2, “Shell Start Up Scripts” provides a brief summary.

Table 4.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

Start up scripts are used to configure your PATH and other environment variables, set your shell prompt and other shell features, create aliases for your favorite commands, 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 by adding the following to your .bashrc:

    export PATH=${PATH}:~/bin
                

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

    setenv PATH ${PATH}:~/bin
                

    If you're 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 be executed. 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.

Practice

Note

Be sure to thoroughly review the instructions in Section 2, “Practice Problem Instructions” before doing the practice problems below.
  1. What are startup scripts?

  2. Are startup scripts sourced by shell processes running other scripts?

  3. What are startup scripts used for?