What Startup Files Are Read by the Shell? (Shell Configuration)
We can customize the behaviour of the shells we use by editing the “startup files” that are read when a shell starts up. These files are often called “dot files” since their names usually start with a dot (.)
Note that the per-user “dot files” are looked for in our home folder (designated by ~) and that they don’t usually exist by default – we have to create them by using a text editor (e.g., ‘pico’, ‘vi’). Note also that since their names start with a dot, these files are not shown when you do ‘ls’ (i.e., list) – we need to use the “-a” option:
to see all files. To list only those files whose names start with a dot, use the command
where .* is the shell pattern for all the files starting with dot and the -d option indicates that directories are listed as plain files not searched recursively)
The following settings are often put in shell startup files:
- shell execution PATH (determines where the shell will look for executables)
- MANPATH (determines where the ‘man’ program will look for man pages)
- shell aliases and functions (used to save you typing)
- shell prompts
- other environment variables
Which startup files are read depends on which shell we are using (e.g. bash or tcsh, and we can check it out by running
if it is bash, it will print out ‘-bash’ for example, and $0 (dollar zero) here is one of the Bash special parameters that expands to the name of the shell or shell script.) and whether it is a ”login shell” (this refers to the shell that is started when we start a new Terminal window) or a “non-login shell” (this refers to the shell we get when we start a sub-shell by typing a shell’s name at the command-prompt). Note however, that sub-shells inherit the environment variables of the parent shell. The following is a summary of the usual startup files read by “interactive” shells (more explanation: An “interactive” shell is the type that we interact with in a Terminal window. But when we run a shell script, a new shell is started for the purpose of that script, and that new shell is a “non-interactive” shell since the user is not interacting with it via the command-prompt). See the relevant ‘man’ page for full details (e.g. ‘man bash’ or ‘man tcsh’).
Note that it isn’t recommended to edit the system supplied startup files (the ones in the “/etc” folder).
Bash Startup Files
When a “login shell” starts up, it reads the file “/etc/profile” and then “~/.bash_profile” or “~/.bash_login” or “~/.profile” (whichever one exists – it only reads one of these, checking for them in the order mentioned).
When a “non-login shell” starts up, it reads the file “/etc/bashrc” and then the file “~/.bashrc”.
Note that when bash is invoked with the name “sh”, it tries to mimic the startup sequence of the Bourne shell (“sh”). In particular, a non-login shell invoked as “sh” does not read any dot files by default. See the bash man page for details.
(note rc could be interpreted as runtime configuration or runcom, .etc)
Tcsh Startup Files
When a “login shell” starts up, it reads the files “/etc/csh.cshrc” and “/etc/csh.login” (in that order) and then “~/.tcshrc” or “~/.cshrc” (whichever one exists – it only reads one of these, checking for them in the order mentioned) and then ~/.login.
When a “non-login shell” starts up, it reads the file “/etc/csh.cshrc” and then “~/.tcshrc” or “~/.cshrc” (whichever one exists – it only reads one of these).
The following sequence shows the startup of several shells (non-login and login) as an illustration of what has been described above. (The ‘head’ command shows the first few lines of a file – see ‘man head’ for details.)
Startup files for shell scripts
There are slightly different rules about which startup files get executed for non-interactive shells (as used in shell scripts) – see the bash man page for details. But if we are writing a shell script, we really shouldn’t rely on any startup files at all - we should make the shell script set up whatever environment it needs on its own. In particular, we should use full paths to all executables and other files referred to by the shell script, so the PATH is not relevant.
Reference: this post adjusted and enriched from the content of http://hayne.net/MacDev/Notes/unixFAQ.html#shellStartup