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
Script | Shells that use it | Executed by |
---|---|---|
/etc/profile, ~/.profile | Bourne shell family | Login shells only |
File named by $ENV (typically .shrc or .shinit) | Bourne shell family | All interactive shells (login and non-login) |
~/.bashrc | Bourne-again shell only | All interactive shells (login and non-login) |
~/.bash_profile | Bourne-again shell only | Login shells only |
~/.kshrc | Korn shell | All interactive shells (login and non-login) |
/etc/csh.login, ~/.login | C shell family | Login shells only |
/etc/csh.cshrc, ~/.cshrc | C shell family | All shell processes |
~/.tcshrc | T-shell | All shell processes |
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:
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.
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.
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.