15.2 Shell Customization
15.2.1 Korn shell customization
The login shell is assigned by the system administrator as part of creating a user account. The login shell determines which initialization files are read during login. The Bourne shell uses only the .profile file to set the user's environment and is read only once at login. The Korn shell uses two user-specific environment files to set the user's environment:
  • ~/.profile (typically contains one-time-only commands and variable definitions)
  • ~/.kshrc (typically contains aliases, turns on shell features and sets the custom prompt)

The .profile File
If a Korn shell user wishes to customize their environment, they need to create or modify the .profile file and they must export the ENV variable as shown below.

The ENV Environment Variable
The ENV variable defines the path to the .kshrc file. This variable must be added anywhere in the .profile file to inform the system that the .kshrc file exists and is to be read when creating a Korn shell.

Command Format:
ENV=$HOME/.kshrc; export ENV

This command sets the environment variable to point to the .kshrc file in the $HOME directory. HOME is a variable that is defined by the system to be the absolute path to the user's login directory. Preceding the HOME variable with the dollar sign ($) metacharacter enables the system to use the value of HOME in the specified location. When the ENV variable is placed in the .profile file and read by other shells (such as the Bourne shell), it does not act as a pointer to the .kshrc file. This variable has no meaning outside of the Korn shell.

The .kshrc File
Many Korn shell commands cannot be executed by the Bourne shell and therefore should not be placed in the .profile file. These Korn shell specific commands and features should be placed in the .kshrc file. The contents of the .kshrc file typically includes:

  • A customized prompt
  • Custom variables
  • Aliases

Figure shows some commands that might be in a .kshrc file. The first command sets the prompt to the machine name of the computer, the second line activates the vi command line editor and the next three commands define the aliases h, c and p. The h alias will display the last 16 lines of command history. The c alias clears the screen, and the p alias will display all processes running for user2. Remember, in order for the settings in the .kshrc file to work, the ENV variable must be exported from the .profile file.

Korn Shell Login Process
Figure illustrates what happens when a Korn shell user logs in to a UNIX system. When any user logs in to the system, the system /etc/profile file is always read first. The Korn shell user's .profile file in their home directory is read next. Then, if the ENV variable in the .profile file is assigned and exported, the .kshrc file is read. The .profile file is only read once, while the .kshrc file is read every time a new Korn shell is opened. Remember the tilde (~) indicates the user's home directory.

To have the system reread the .kshrc or .profile file after changes have been made, the user can either log out and log back in, or type the following from the command line:

$ . ~/.kshrc
$ . ~/.profile

Interactive Media Activity (Flash, 91 kB)
  Customizing the Korn Shell

In this media activity, you are logged in as user2 and your current working directory is: /home/user2. Type the commands that would accomplish the requested objectives. 
Note: Be sure to press enter after each one. Click on step 1 to begin.

Lab Activity (Adobe Acrobat Reader, 55 kB)
  Customizing the Korn Shell

In this lab, you will work with UNIX initialization files to customize your Korn shell login environment and tailor various options to your needs. In the 14.2.5 lab, you worked with aliases and custom prompts that were only active for the current session. When you closed the terminal window or logged out, the custom settings were lost. These and other features can be automatically made available each time you login by modifying specific initialization files. In this lab you will work with various initialization files to make these changes take effect each time you login. You will also review system-wide and user-specific initialization files and variables.