3333333333333333333333 This chapter functions as a reference source for C, Bourne, and Korn shell
8.4 Korn Shell Features
8.4.1 Sample profile and kshrc Login Scripts
If your login shell is the Korn shell, the operating system processes the .profilelogin script in your home directory. The.profilelogin script defines environment variables. These variables are used by your login shell as well as any subshells and subprocess that are created. The
.profilelogin script is executed only when you log in.
The.kshrc login script sets up your Korn shell environment by defining variables and operating parameters for the local shell process. It is executed each time you create a subshell.
Note
Before creating a.kshrcfile in your home directory, make sure that theENV=$HOME/.kshrc environment variable is set and exported in your.profile. Once this is done, the .kshrc login script will execute each time you log in and each time you create a subshell.
In the following.profilelogin script, global environment variables are set and exported, and shell variables are set. Table 8-12 explains each part of the script.
# Set environment variables PATH=/usr/bin:/usr/local/bin: ENV=$HOME/.kshrc
EDITOR=vi FCEDIT=vi
PS1="’hostname’ [!] $ " # Export global variables
export PATH ENV EDITOR FCEDIT PS1 # Set mail variables
MAIL=/usr/spool/mail/$LOGNAME MAILCHECK=300
Table 8-12: Example Korn Shell .profile Script
22222222222222222222222222222222222222222222222222222222222222222222222
Command Description
22222222222222222222222222222222222222222222222222222222222222222222222
Set Environment Variables
22222222222222222222222222222222222222222222222222222222222222222222222 PATH=/usr/bin:/usr/local/bin Specifies the search path. In this
case,/usr/binis searched first and/usr/local/bin searched second.
ENV=$HOME/.kshrc Specifies$HOME/.kshrcas
the login script.
EDITOR=vi Specifiesvias the default
editor for command line editing at the shell prompt and for filename completion.
FCEDIT=vi Specifiesvias the default
editor for thefc command.a
PS1="‘hostname‘ [!] $ " PS1is the variable that
specifies the Korn shell prompt, and its default value is$. However, this variable assignment specifies that your prompt should be changed to the following: the output of the hostnamecommand, followed by the command number of the current command, followed by the dollar sign ($). For example, if the name of your system isboston, and the current command is numbered 30, your prompt would be the following: boston[30] $. 22222222222222222222222222222222222222222222222222222222222222222222222
Export Global Variables
22222222222222222222222222222222222222222222222222222222222222222222222 export PATH ENV EDITOR FCEDIT PS1 Specifies that the values of the
PATH,ENV,EDITOR,FCEDIT, andPS1variables should be exported to all subshells.
Table 8-12: (continued)
22222222222222222222222222222222222222222222222222222222222222222222222
Command Description
22222222222222222222222222222222222222222222222222222222222222222222222
Set Mail Variables
22222222222222222222222222222222222222222222222222222222222222222222222 MAIL=/usr/spool/mail/$LOGNAME Specifies the pathname of the
file used by the mail system to detect the arrival of new mail. In this case, the mail system would look in your username subdirectory under the
/usr/spool/maildirectory.
MAILCHECK=300 Specifies that the shell should
check for mail every 300 seconds (5 minutes).
22222222222222222222222222222222222222222222222222222222222222222222222 Table notes:
a. For information on thefccommand, see Section 8.4.4.
In the following.kshrclogin script, shell variables, command aliases, and command history variables are set, as well as the permissions for file
creation. Table 8-13 explains each part of the script.
# Set shell variables set –o monitor
set –o trackall # Set command aliases alias rm=’rm –i ’ alias rename=’mv ’
alias h ’history \!* | more’ alias l ’ls –l’
alias c clear
# Set history variables HISTSIZE=40
# Set file creation permissions umask 027
Table 8-13: Example .kshrc Script 222222222222222222222222222222222222222222222222222222222222222222222222222 Command Description 222222222222222222222222222222222222222222222222222222222222222222222222222 Shell Variables 222222222222222222222222222222222222222222222222222222222222222222222222222 set –o monitor Specifies that the shell should monitor all
background processes and display a completion message when the process finishes.
set –o trackall Specifies that the shell should track all commands that you execute. Once a command is tracked, the shell stores the location of the command and finds the command more quickly the next time you enter it.
222222222222222222222222222222222222222222222222222222222222222222222222222
Command Aliases
222222222222222222222222222222222222222222222222222222222222222222222222222 alias rm=’rm –i’ Specifies the use of the–ioption (which
prompts you for file deletion) with therm command.
alias rename=’mv’ Specifiesrenameas a new name for the mvcommand.
alias h=’history’ \!* | more’ Defines a command that pipes the contents of the command history buffer through the morecommand. The\!* string specifies that all of the history buffer should be piped.
alias l=’ls –l’ Defines a short name for thels –l command that lists directory files in the long format.
alias c=’clear’ Defines a short name for the clear command that clears your screen.
222222222222222222222222222222222222222222222222222222222222222222222222222
History Variables
222222222222222222222222222222222222222222222222222222222222222222222222222 HISTSIZE=40 Instructs the shell to store the last 40
commands in the history buffer.
Table 8-13: (continued)
222222222222222222222222222222222222222222222222222222222222222222222222222
Command Description
222222222222222222222222222222222222222222222222222222222222222222222222222
Set File Creation Permissions
222222222222222222222222222222222222222222222222222222222222222222222222222
umask 027 Specifies the maximum permissions for all
new files created. This command provides all permissions for the owner, read, and execute permissions for members of the same group, and no permissions for all others. The umask is not inherited by subshells.
222222222222222222222222222222222222222222222222222222222222222222222222222
8.4.2
Metacharacters
Table 8-14 describes Korn shell metacharacters (characters that have special meaning to the shell).
Table 8-14: Korn Shell Metacharacters
222222222222222222222222222222222222222222222222222222222222222222 Metacharacter Description
222222222222222222222222222222222222222222222222222222222222222222
Syntactic
222222222222222222222222222222222222222222222222222222222222222222 | Separates commands that are part of a pipeline.
&& Runs the next command if the current command succeeds. | | Runs the next command if the current command fails. ; Separates commands that should be executed sequentially. ;; Separates elements of a case construct.
& Runs commands in the background.
( ) Groups commands in a subshell as a separate process. { } Groups commands without creating a subshell.
222222222222222222222222222222222222222222222222222222222222222222
Filename
222222222222222222222222222222222222222222222222222222222222222222 / Separates the parts of a file’s pathname.
? Matches any single character except a leading dot (.). * Matches any character sequence except a leading dot (.). [ ] Matches any of the enclosed characters.
~ Specifies a home directory when it begins a filename.
Table 8-14: (continued) 222222222222222222222222222222222222222222222222222222222222222222 Metacharacter Description 222222222222222222222222222222222222222222222222222222222222222222 Quotation 222222222222222222222222222222222222222222222222222222222222222222 \ Specifies that the following character should be interpreted
literally; that is, without its special meaning to the shell. ’...’ Specifies that any of the enclosed characters (except for
the’) should be interpreted literally; that is, without their special meaning to the shell.
"..." Provides a special form of quoting. Specifies that the dollar sign ($), ‘ (grave accent), \ (backslash), and ) (close parenthesis) characters keep their special meaning, while all other enclosed characters are interpreted literally; that is, without their special meaning to the shell. Double quotes (" ") are useful in making variable assignments. 222222222222222222222222222222222222222222222222222222222222222222
Input/Output
222222222222222222222222222222222222222222222222222222222222222222 < Redirects input.
> Redirects output to a specified file.
<< Redirects input and specifies that the shell should read input up to a specified line.
>> Redirects output and specifies that the shell should add output to the end of a file.
>& Redirects both diagnostic and standard output and appends them to a file.
222222222222222222222222222222222222222222222222222222222222222222
Substitution
222222222222222222222222222222222222222222222222222222222222222222 ${...} Specifies variable substitution.
% Specifies job number substitution. ‘...‘ Specifies command output substitution.
222222222222222222222222222222222222222222222222222222222222222222
8.4.3
Command History
The command history buffer stores the commands you enter and allows you to display them at any time. As a result, you can select a previous command, or parts of previous commands, and then reexecute them. This feature may save you time because it allows you to reuse long commands instead of reentering them.
To see the contents of the history buffer, use thehistorycommand. The displayed output will be similar to the following (your output will vary):
[18] $ history 3 ls –l 4 pwd 5 cd /usr/sales 6 ls –l 7 cp report report5 8 mv /usr/accounts/new . 9 cd /usr/accounts/new 10 mkdir june 11 cd june 12 mv /usr/accounts/new/june . 13 ls –l 14 cd /usr/sales/Q1 15 vi earnings 16 cd /usr/chang 17 vi status [19] $
To reexecute any command in the command history buffer, use the commands listed in Table 8-15. Each command starts with the letter r.
Table 8-15: Reexecuting History Buffer Commands
2222222222222222222222222222222222222222222222222222222222222222222 Command Description
2222222222222222222222222222222222222222222222222222222222222222222 r Reexecutes the previous command
r n Reexecutes the command specified byn. For example, using the history buffer shown in the previous display,r 5reexecutes thecd /usr/salescommand.
r –n Reexecutes a previous command relative to the current command. For example, using the history buffer shown in the previous display,r-2invokes command number16,cd /usr/chang.
r string Reexecutes the most recent command that has first characters matching those specified bystring. For example, using the history buffer shown in the previous display,r cpinvokes command number7,cp report report5.
2222222222222222222222222222222222222222222222222222222222222222222 For more information on reexecuting history buffer commands, see the ksh(1) reference page.
If you want to increase or decrease the number of commands stored in your history buffer, set theHISTSIZEvariable in your.profile file. This variable has the following format:
HISTSIZE=n
Thenentry specifies the number of command lines you want to store in the history buffer.
For example, to store 15 commands in the history buffer, use the following command:
HISTSIZE=15
The Korn shell also allows you to edit current command lines as well as reuse those already entered in the command history buffer. To use this feature, you must know how to use a text editor such asvioremacs. For information on these features, see the following section.