14.2 Korn Shell Features
14.2.5 Custom prompts with the Korn shell
The Korn shell uses the Bourne shell as a basis for its features. These shared features include a login initialization file and variables (a place holder for information used for customizing the shell). In addition, the Korn shell provides aliasing, command tracking, command-line editing, and command substitution. These features allow the user to be more productive. The initialization file used to store this information is the .kshrc (Korn shell run control) file and is stored in the user's home directory.

The default prompt for the Korn shell is the dollar sign ($). The user can customize their own shell prompt to their liking using the PS1 (Prompt String 1) predefined shell prompt variable (PS1=$). PS1 can include a wide range of expressions such as character strings, commands, or other variables. Figure shows several different ways the standard PS1 prompt ($) can be customized. The variable PS1 is a shell variable. Any change in the variable setting will remain until the shell is exited or until a subshell is opened. To make the customized prompt available from one session to the next, place the PS1 variable in the .kshrc file (covered in Chapter 15).
Command Format:
PS1=value
 
  1. $ PS1="Good morning$ " - The first example assigns the prompt to a character string. The prompt is changed from the conventional $ to a greeting (Good morning) with the $ at the end. The double quotes are necessary because of the space between 'good' and 'morning.'
  2. Good morning$ PS1="`uname -n` !$ " - This example demonstrates using a command (uname -n) with the history line number (!) for a unique prompt. This command causes the prompt to show the name of the host on which you are working (saturn 41). The back quotes ( ` ) are used to substitute the output of the uname command instead of interpreting it literally. Double quotes surround the entire string.
  3. saturn 41? PS1="$ " - This example sets the prompt back to the original Korn shell dollar sign ($) prompt.
  4. $ PS1="$PWD $" - In this example, the prompt is customized to reflect the current working directory. Double quotes surround the string and allow expansion of the $PWD variable which always keeps track of the current working directory.
Interactive Media Activity (Flash, 93 kB)
  Customizing your Korn Shell Prompt

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, 57 kB)
  Korn Shell Features

In this lab, you will work with the Korn shell to understand its features and capabilities. You will work with aliases, history, and re-execution of commands and custom prompts in this lab.