Configuring the Shell


When your login shell starts up, it looks for certain files in your home directory. These files contain commands that can be used to configure your working environment. The particular files it looks for depend on which shell you are using:

  • sh runs the commands in a configuration file called .profile.

  • ksh also uses the .profile file. In addition, you can set a variable in your .profile to cause it to read the commands in a second file. The variable is called ENV. By convention, that file is often called .kshrc.

  • bash uses the file .bash_profile. If that file does not exist, it will look for the file .profile, instead. The .bash_profile often contains a line that causes bash to run the commands in a second file, bashrc. When you log out of bash, it will run the commands in .bash_logout.

  • csh looks for a file called .login. It will also run commands in .cshrc. When you log out of the C shell, it will run the commands in the file .logout.

  • tcsh uses the .login file as well. It also looks for .tshrc. If that file does not exist, it will look for .cshrc instead. Like the C shell, tcsh will run the .logout file when you log out.

This may all sound very confusing, but these configuration files all work in pretty much the same way Each of these files is actually an example of a shell script. They contain commands or instructions for the shell. The commands include settings that allow you to customize your environment.

The first file that the shell reads (.profile, .bash_profile, or .login) contains variables or settings that you want to be in effect throughout your login session. The section “Shell Variables” later in this chapter describes these variables. The file might also include commands you want to run at login, such as cal (to display a calendar for the current month) or who (to show the list of users who are currently logged in).

The newer shells support a second configuration file, which is used for defining command aliases,functions, and certain shell variables. This file is usually called .kshrc, .bashrc, .cshrc, or .tcshrc, depending on which shell you are using. (The rc stands for “read commands.” By convention, programs often look for initialization information in files ending in rc. Other examples are .exrc, which is used by vi, and .mailrc, used by mailx.)

Interactive Shells

You can start another shell after you log in by using the name of the shell as a command; for example, to start the Korn shell, you could type ksh at the command prompt. This type of shell is not a login shell, and you do not have to log in again to use it, but it is still an interactive shell, meaning that you interact with the shell by typing in commands (as opposed to using the shell to run a script, as discussed in Chapter 20). The instances of the shell that run in a terminal window when you are using a graphical interface are also interactive non-login shells. When you start a non-login shell, it does not read your .profile, .bash_profile, or .login file (or your .logout file), but it will still read the second shell configuration file (such as .bashrc). This means that you can test changes to your .bashrc by starting another instance of the shell, but if you are testing changes to your .profile or .login, you must log out and then back in to see the results.

Sample Configuration Files

If you define a variable by typing its new value on the command line, it will return to its previous value the next time you log in. In order to keep important variables such as PATH, PS1, and TERM defined every time you log in, they are usually included in one of your configuration files. Here are a few examples of those files. Don’t worry if the commands don’t make sense yet-you can come back to this later, after reading the sections “Shell Variables” and “Command Aliases.”

If you want to edit your shell configuration file, you will probably need to use a text editor, such as vi or emacs. These programs are explained in detail in Chapter 5.

The shell does not try to interpret lines that begin with #, or any text following a #. You can use this to include comments in your configuration files. A comment is just a note to yourself, to help you remember how the commands in your file work.

Sample .bash profile

Every time you log in, bash reads your .bash_profile. This file includes definitions of environment variables that will be shared with other programs and commands such as who that you want to run at the beginning of each login session. A typical .bash_profile might look some-thing like this:

 # .bash_profile - example # set environment variables export TERM=vt100 export PATH=$PATH:/sbin:/usr/sbin:$HOME/bin export MAILCHECK=30 # allow incoming messages from other users mesg y # make sure backspace works stty erase "^H" # show all users who are currently logged in who # load aliases and local variables . -/.bashrc

The last line executes your .bashrc file. If you leave it out, bash will not read your aliases and variable definitions when you log in.

A .profile for ksh would look almost identical. The only important change would be to the lines at the end that execute the .bashrc file. These would be replaced by a line like

 export ENV=$HOME/.kshrc

This will cause ksh to look for other configuration settings in the file .kshrc in your home directory Note that ENV may be set to any filename, although $HOME/.kshrc is a common choice.

Sample .bashrc File

When you start an interactive bash shell after logging in (e.g., by opening an xterm window), it reads the commands in your .bashrc file. This file includes commands and definitions that you want to have executed every time you run a shell-not just at login. The .bashrc file defines local shell variables (but not environment variables, which belong in .bash_profile), shell options, and command aliases. It might look something like this:

 # .bashrc file-example # set shell variables (such as the prompt) PS1="\u \w> " # set shell options set −o noclobber set −o emacs set +o notify # set default file permissions umask 027 # define aliases alias lg='ls -g -color=tty' alias r='fc -s' alias rm='rm -i' alias cp='cp -r' alias wg='who | grep' alias hibernate='sudo apm -s'

A configuration file for ksh could look like this, as well. However, the line PS1=“\u \w>” in this example would have to be changed to PS1=‘$LOGNAME $(pwd)> ’, because ksh doesn’t support the bash shortcuts for defining the prompt.

Sample .login File

As the name suggests, tcsh reads the .login file only when you log in. Your .login file should contain commands and variable definitions that only need to be executed at the beginning of your session. Examples of things you would put in .login are commands for initializing your terminal settings, commands such as date that you want to run at the beginning of each login session, and definitions of environment variables.

The following is a short example of what you might put in a typical .login file:

 # .login file-example # show number of users on system echo "There are" who wc -l "users on the system" # set terminal options-- in particular, # make sure that the Backspace key works stty erase "^H" # set environment variables setenv term vt100 setenv mail ( 60 /var/spool/mail/$user)

These examples illustrate the use of setenv, the C shell command for defining environment variables. setenv and its use are discussed further later on, in the section “csh and tcsh Variables.”

Sample .tcshrc File

The difference between .tcshrc and .login is that tcsh reads .login only at login, but it reads .tcshrc both when it is being started up as a login shell and when it is invoked as an interactive non-shell. The .tcshrc file includes commands and definitions that you want to have executed every time you run a shell-not just at login.

Your .tcshrc should include your alias definitions, and definitions of variables that are used by the shell but are not environment variables. Environment variables should be defined in .login.

 # .tcshrc file-example # set shell variables set path = ($path /sbin /usr/sbin /usr/bin /$home/bin) set prompt = "[%n@%m %c] tcsh % " # turn on ignoreeof and noclobber # turn off notify set ignoreeof set noclobber unset notify # define aliases alias lsc ls -Ct alias wg 'who | grep' alias rm rm -i alias cp cp -r alias hibernate sudo apm -s # set permissions for file creation umask 027

This sample file includes C shell variable definitions and aliases, both of which are explained in the following sections.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

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