Project 47. Customize Shell Startup"How do I get my Bash customization commands to be executed automatically whenever I launch a new shell?" This project explores Bash configuration files. Configuration files are simply Bash shell scripts that Bash automatically executes during its startup phase. We learn where to place customization commandsboth global commands that apply to all users and private commands that apply to an individual user. We also learn about the difference between login and non-login shells, and how this affects the configuration files. The last section covers Tcsh shell equivalents. Learn More
Configure Login ShellsWhen we start a new Terminal window, Terminal launches a new instance of the Bash shell. During its startup phase, the Bash shell executes commands from a configuration file called /etc/profile. It is to this file that we write customization commands to do any, or all, of the following:
The /etc/profile script file is executed no matter which user opens a new Terminal window and must be edited as root with the sudo command. Should you wish to apply personal customization settings on top of those set in /etc/profile, place them in the configuration file .bash_profile in your home directory (~/.bash_profile). You may add to or change these settings without affecting other users. Tip
Configure Non-Login ShellsTerminal performs a full Unix login whenever you open a new Terminal window and always launches what's called a login shell. A login shell starts in a clean environment, setting environment variables such as HOME and USER. A command (such as ls) launched from the shell does not go through the login process but runs within the parent shell process, inheriting its environment. When you launch a new instance of Bash from the command line by typing $ bash Note
this, too, runs within the current shell, inheriting its environment. Bash launched in such a manner is called a non-login shell and starts up differently from a login shell. It does not need to execute customization commands that affect its environment, because it's already inherited the environment. A non-login shell, therefore, does not execute the login configuration scripts in /etc/profile and ~/.bash_profile ; instead, it executes two equivalent non-login configuration scripts, the files /etc/bashrc and ~/.bashrc. You should use the non-login configuration scripts only for the following customizations, with the same global/personal split we saw for login shells:
Avoid the duplication of having to include the same configuration commands in both sets of configuration files by adopting the following policy. In the login scripts /etc/profile and ~/.bash_profile, we write commands to:
Learn More
In the non-login scripts /etc/bashrc and ~/.bashrc, we write commands to:
Note
Finally, we add commands to execute the corresponding non-login script from each login script. Then we simply have the login scripts fetch the appropriate settings from the non-login scripts. (The default /etc/profile supplied with Mac OS X already does this.) Here's what to do:
Noninteractive ShellsLogin and non-login shells are also interactive shells, meaning that they are launched to service the command line. A shell launched to run a shell script, which exits when the script completes, is termed a noninteractive shell. It does not execute any configuration scripts and, therefore, will inherit its parent's environment but won't set up any of configuration of its own. The Tcsh ShellThe Tcsh shell has a similar global/local and login/non-login split. A login shell executes the following script files, and in this order. /etc/csh.cshrc /etc/csh.login ~/.tcshrc ~/.login A non-login shell executes following script files, and in this order. /etc/csh.cshrc ~/.tcshrc A noninteractive Tcsh shell executes /etc/csh.cshrc and ~/.tcshrc, unlike a Bash noninteractive shell, which does not execute any configuration scripts. Many commands in ~/.tcshrc are not necessary for noninteractive shells, such as setting shell variables and aliases. We filter these commands for noninteractive shells like this. Include this statement near the top of ~/.tcshrc if ($?prompt) set interactive and surround all interactive-only commands with if ($?interactive) then ... interactive only commands are here... endif |