Project47.Customize Shell Startup


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

Project 2 covers the sudo command, and the projects in Chapter 4 show you how to create and edit files by using several Unix text editors.


Configure Login Shells

When 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:

  • Set environment variables such as PATH (Project 50)

  • Define and export functions (Project 52)

  • Define local functions (Project 52) and aliases (Project 51)

  • Set shell variables such as the prompt (Project 45) and history settings (Project 48)

  • Set shell options and attributes (Project 45)

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

To start Bash from the command line and make it act as though it had been invoked as a login shell, specify the option -l or --login.


Configure Non-Login Shells

Terminal 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

It's important to ensure that commands that add to an existing environment variable are executed once onlythat is, they must be run by login shells only. For example, this extension to the PATH environment variable must be executed just once.

$ PATH=$PATH:~/bin



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:

  • Define local functions and aliases

  • Set shell variables

  • Set shell options and attributes

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:

  • Set environment variables

  • Define and export functions

Learn More

Project 9 tells you more about writing simple shell scripts.


In the non-login scripts /etc/bashrc and ~/.bashrc, we write commands to:

  • Define local functions and aliases

  • Set shell variables

  • Set shell options and attributes

Note

Remember that the tilde ( ~ ) symbol is used as shorthand for the absolute pathname of your home directory. ~/.bashrc means the file .bashrc in your home directory.


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:

  • Add this line to the end of /etc/profile :

    source /etc/bashrc

  • Add this line to the end of ~/.bash_profile :

    source ~/.bashrc

X11 and xterm

If you use X11 and xterm, be warned that xterm, unlike Apple's Terminal, does not start a login shell. To force a login shell, either invoke xterm with the option -ls or add this line to the file ~/.Xdefaults.

XTerm*.LoginShell: True



Noninteractive Shells

Login 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 Shell

The 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





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net