It's now time to discuss how to customize your shell to do what you want. You can create aliases to simplify common commands, you can automatically execute certain programs every time you log in, you can set various environment variables to your taste, and much more. These tasks are handled in the shell initialization and configuration files, which exist both at the global and per-user levels. Because tcsh and bash have completely different sets of configuration files, we will cover them both sequentially. FreeBSD includes systemwide and default per-user config files for both shells so that you can switch from tcsh to bash on a system basis if you really want to (although this is rarely necessary). Note When a new user is created, default shell config files are copied from /usr/share/skel (the dot prefix on each of the files is dropped) into the new user's home directory. If you like, you can copy these default files into /usr/local/share/skel, modify them to your taste, and then alter the /etc/adduser.conf file to copy its default "dotfiles" from this new location. Use this technique to make a global, preemptive change to all users' default shell configurations before they're even created. tcsh/csh Files: .cshrc, .login, and .logoutThe first file that tcsh (or csh) looks at when it is executed is the systemwide config file, /etc/csh.cshrc, followed immediately by /etc/csh.login. Note that in FreeBSD, both these files exist, but their contents are commented out (see Listing 9.1). Listing 9.1. The Global /etc/csh.cshrc and /etc/csh.login Files
This listing shows that these config files don't really do anything, but they could if you so chose. Any change you make in either of these files, such as enabling mesg (terminal messages) globally, is applied globally (for all users running csh or tcsh, at least). The change is overridden only if set differently by the individual user config files, which function similarly to systemwide ones and are read immediately afterward from the user's home directory (.cshrc, followed by .login). The default .cshrc file, which comes from /usr/share/skel/dot.cshrc, does a number of generally useful things to set up the shell environment. For example, it creates several aliases to shorten commands: alias h history 25 alias j jobs -l alias la ls -a alias lf ls -FA alias ll ls -lA It sets the search path for programs:
It sets various environment variables that control the behavior of many different shell tasks: setenv EDITOR vi setenv PAGER more setenv BLOCKSIZE K And a few other things are set as well. You can look at the .cshrc file in your own home directory to see them all. The .login file, which is read next, is where the user can place any programs he wants to run every time he logs in. For example, the fortune program is provided in the default .login file (from /usr/share/skel/dot.login) and is encapsulated in an existence-test conditional, so that a random witticism or FreeBSD usage tip is emitted every time the user logs in: [ -x /usr/games/fortune ] && /usr/games/fortune freebsd-tips You (the administrator) or the user can also specify certain things to happen whenever the user logs out. For instance, you might write a script to clean out any temporary files owned by the user in /tmp; you could put a call to this script into /etc/csh.logout, which in its default state has no material contents: # cat /etc/csh.logout # $FreeBSD: src/etc/csh.logout,v 1.3 1999/08/27 23:23:41 peter Exp $ # # System-wide .logout file for csh(1). The logout process will also read a .logout file in the user's home directory if it exists, but there is no such per-user file installed by default. Note The files shown in this section are the ones run by tcsh when it is executed as a login shell. There are various other circumstances under which tcsh can be runfor example, as a non-login shell executed in order to run a C shell script, invoked from the interpreter line of the script. In this case, the .login and .logout files (and their systemwide equivalents) are ignored. bash Files: .profile, .shrc, and .bash_logoutIf you've chosen to use bash rather than tcsh as your shell, it will operate in much the same way: it will first read the systemwide config and initialization files and then proceed to the per-user ones. First comes /etc/profile, which (like /etc/csh.cshrc) is materially blank but contains a few examples for options you might decide to enable, as shown in Listing 9.2. Listing 9.2. The Global /etc/profile File
Notice that /etc/profile combines the functionality of both /etc/csh.cshrc and /etc/csh.login. It's the only bash-related global file in the system; there is no systemwide logout script for bash. The next file for bash is the user's .profile file, which primarily sets various environment variables (including PATH) and exports them in the Bourne shell style:
The second-to-last line sets the ENV variable to the .shrc file, also in the user's home directory. This is then read in sequence with .profile. Its purpose is to define command aliases similar to their counterparts in .cshrc, as well as a few other optional things, such as customizing the prompt, that are commented out by default: # some useful aliases alias h='fc -l' alias j=jobs alias m=$PAGER alias ll='ls -laFo' alias l='ls -l' alias g='egrep -i' # # be paranoid # alias cp='cp -ip' # alias mv='mv -i' # alias rm='rm -i' # # set prompt: ``username@hostname$ '' # PS1="`whoami`@`hostname | sed 's/\..*//'`" # case `id -u` in # 0) PS1="${PS1}# ";; # *) PS1="${PS1}$ ";; # esac # search path for cd(1) # CDPATH=.:$HOME As with .logout for tcsh, bash will read and execute a .bash_logout file if it's present (but no such file is installed by default). This functionality is designed for users' convenience rather than administrators' security; there are many ways in which a user can exit the shell, and there's no guarantee that the logout script will be properly executed every time. Note These are the files "sourced" (that is, read as configuration files) by bash if it's executed as a login shell. If it's used as a non-login shell, though, the file it reads is .bashrc rather than .profile. This file doesn't normally exist in FreeBSD. |