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
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 |
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:
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.
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.
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.
What are startup scripts?
Are startup scripts sourced by shell processes running other scripts?
What are startup scripts used for?