Donation?

Harley Hahn
Home Page

Send a Message
to Harley


A Personal Note
from Harley Hahn

Unix Book
Home Page

SEARCH

List of Chapters

Table of Contents

List of Figures

Chapters...
   1   2   3
   4   5   6
   7   8   9
  10  11  12
  13  14  15
  16  17  18
  19  20  21
  22  23  24
  25  26

Glossary

Appendixes...
  A  B  C
  D  E  F
  G  H

Command
Summary...

• Alphabetical
• By category

Unix-Linux
Timeline

Internet
Resources

Errors and
Corrections

Endorsements


INSTRUCTOR
AND STUDENT
MATERIAL...

Home Page
& Overview

Exercises
& Answers

The Unix Model
Curriculum &
Course Outlines

PowerPoint Files
for Teachers

Exercises and Answers for Chapter 14...

Using the Shell: Initialization Files

Review Question #1:

What is an initialization file?

Name the two types of initialization files.

What is a logout file?

Answer

An initialization file is a file that contains commands to be executed when a user logs in or when a new shell starts.

There are two types of initialization files. A login file runs whenever a user logs in; an environment file runs whenever a new shell starts.

A logout file contains commands that are executed when a user logs out.

Review Question #2:

What is a dotfile?

What is an rc file?

Answer

A dotfile is a file whose name begins with a . (dot) character. When the ls command lists filenames, dotfiles are not listed unless requested specifically by using the -a (all files) option. For this reason, dotfiles are sometimes called hidden files.

An rc file is a file whose name ends with the letters "rc" (which stands for "run commands"), for example, .bashrc. Such files are usually dotfiles, and are used as configuration files to hold commands that are run automatically each time a particular program starts.

Review Question #3:

What is a login shell? What is a non-login shell?

Why is the distinction important?

Answer

A login shell is the shell that is started automatically whenever you log in. Any other interactive shell is a non-login shell.

The distinction is important because initialization files are processed differently for login shells than for non-login shells: a login shell executes your login file and your environment file; a non-login shell only executes your environment file.

The distinction is also important because you can set a shell option to not terminate a shell when you press ^D, if that shell is your login shell. This setting keeps you from logging out accidentally.

Review Question #4:

You have a list of favorite aliases you like to use, so you decide to put the definitions in one of your initialization files. Which file will they go in, the login file or the environment file? Why?

What else goes in this file?

Answer

Aliases are not part of the environment. Thus, when you start a new shell, it will not inherit your aliases. For this reason, alias definitions must go in the environment file, where they will be executed every time a new shell starts.

In general, the job of the environment file is to set up whatever customizations cannot be stored in the environment. In particular, shell options, aliases and functions must be recreated whenever a new shell starts.

Applying Your Knowledge #1:

Look carefully in your home directory to see if you already have a login file and an environment file. (You can use the command ls -a to list your dotfiles.) If so, take a look at what is inside each file. (Either use the less command or open the files in your text editor.)

Answer

To list your dotfiles use either:

ls -a
ls -a | less

To look inside the file .foo use either:

less .foo
vi .foo

Applying Your Knowledge #2:

Create (or modify) a login file for yourself using the sample file in either Figure 14-3 or 14-6 as a template. If you have an existing file, save a copy under a different name as soon as you open it within your text editor, before you make any modifications. That way, if you make a mistake, you will be able to change back to the original version.

Answer

For Bash: .bash_profile
For Korn Shell: .profile
For Tcsh: .login
For Csh: .login

Applying Your Knowledge #3:

Create (or modify) an environment file for yourself using the sample file in either Figure 14-4 or 14-5 as a template. If you have an existing file, make a backup copy as described in the previous exercise.

Answer

For Korn Shell: .kshrc
For Tcsh: .tcshrc
For Csh: .cshrc

With the Korn Shell, you must include the following line in your login file (.profile ):

export ENV=${HOME}/.kshrc

Applying Your Knowledge #4:

Create a logout file for yourself. If you are not sure what to put in it, use the echo command to say goodbye to yourself. The name you use for this file depends on which shell you are using (see Figure 14-1). If you are using the Korn shell, you will have to trap the exit signal (explained in the chapter and in Figure 14-3).

Answer

For Korn Shell: .logout
For Tcsh: .logout
For Csh: .logout

With the Korn Shell, you must include the following line in your login file (.profile ):

trap '. ${HOME}/.logout; exit' EXIT

To test your logout file, include the following command:

echo "Goodbye. Have a nice day."

For Further Thought #1:

The POSIX standard mandates that a shell should support a login file and an environment file, but not necessarily a logout file. This implies that the logout file is less important than the other two files. Why should this be the case?

Answer

The login file and environment file perform initializations that control your actual work experience. The login file performs one-time initializations; the environment file performs initializations each time you start a new shell. Without these files, you would have much less control over your work environment.

The logout file is only executed after you have already finished work. Thus, it's effects are much less important.

For Further Thought #2:

On many systems, when a new account is created the new userid will automatically be given a default login file and, sometimes, a default environment file. Why is this a good idea?

Would you advise a new user to modify these files or leave them alone?

Answer

It is a good idea to give all new users default login and environment files because it allows the system administrator to create initialization files that will set up a proper working environment for everyone, for example, by specifying the search path.

If a new user knows what he is doing, he should examine the default initialization files and modify them as he wishes. Otherwise, he should leave them alone, so he doesn't inadvertently cause a problem.

Jump to top of page