This chapter functions as a reference source for C, Bourne, and Korn shell features. Unlike other chapters of this guide that present conceptual and/or tutorial information, the purpose of this chapter is to provide very brief reference information about each shell.
To get the most out this chapter, you should already be familiar with the introductory shell overview information in Chapter 7.
After completing this chapter, you should be able to:
Table 8-1 compares C, Bourne, and Korn shell selected features.
Feature | Description | C | Bourne | Korn |
Shell programming | A programming language that includes features such as loops, condition statements, and variables. | Yes | Yes | Yes |
Signal trapping | Mechanisms for trapping interruptions and other signals sent by the operating system. | Yes | Yes | Yes |
Restricted shells | A security feature that provides a controlled shell environment with limited features. | No | Yes | No |
Command aliases | A feature that allows you to abbreviate long command lines or to rename commands. | Yes | No | Yes |
Command history | A feature that stores commands and allows you to edit and reuse them. | Yes | No | Yes |
Filename completion | A feature that allows you to enter a portion of a filename and the system automatically completes it or suggests a list of possible choices. | Yes | No | Yes |
Command line editing | A feature that allows you to edit a current or previously entered command line. | Yes | No | Yes |
Array | The ability to group data and call it by a name. | Yes | No | Yes |
Integer arithmetic | The ability to perform arithmetic functions within the shell. | Yes | No | Yes |
Job control | Facilities for monitoring and accessing background processes. | Yes | No | Yes |
For detailed information on shell features, see the appropriate shell reference pages; sh(1), csh(1), or ksh(1).
This section describes the following C shell features:
The .cshrc login script sets up your C shell environment by defining variables and operating parameters for the local shell process. The .login script defines variables and operating parameters that you want executed at the beginning of your session, and that you want to be valid for all shell processes during the current login session.
When you log in, the operating system executes the .cshrc file in your home directory first, and the .login file second. The .login script is executed only when you log in. However, the .cshrc file is executed each time you create a subshell.
In the following .cshrc script, shell variables, command aliases, and command history variables are set. Table 8-2 explains each part of the script.
# Set shell variables set noclobber set ignoreeof set notify
# Set command aliases alias h 'history \!* | more' alias l 'ls -l' alias c clear
# Set history variables set history=40 set savehist=40
# Set prompt set prompt = '\! % '
Command | Description |
Shell Variables | |
set noclobber | Stops files from being overwritten. If set, places restrictions on output redirection > to ensure that files are not accidentally destroyed, and that >> redirections refer to existing files. |
set ignoreeof | Specifies that you cannot use Ctrl/D to end your login session. Instead, you must use either the exit or the logout commands. |
set notify | Informs you when background processes have completed. |
Command Aliases | |
alias h 'history \!* | more' | Defines the contents of the command history buffer through the more command. The \!* string specifies that all the history buffer should be piped. |
alias l 'ls -l' | Defines a short name, l, for the ls -l command that lists directory files in the long format. |
alias c clear | Defines a short name, c, for the clear command that clears your screen. |
History Variables | |
history=40 | Instructs the shell to store the last 40 commands in the history buffer. |
savehist=40 | Instructs the shell to store the last 40 commands and use them as the starting history for the next login session. |
Prompt Variable | |
set prompt = '\! % ' | Changes your prompt so that it tells you the command number of the current command. |
In the following .login script, the permissions for file creation are set, the PATH environment variable is set, and the editor and printer are specified. Table 8-3 explains each part of the script.
# Set file creation permissions umask 027
# Set environment variables setenv PATH=/usr/bin:/usr/local/bin: set cdpath=.:..:$HOME setenv EDITOR emacs setenv MAILHOST boston setenv PRINTER sales
Command | Description |
File Permissions | |
umask 027 | Specifies the permissions to be subtracted from the default permissions set by the creating program for all new files created. The umask value is subtracted from 777 (for executable programs) or from 666. For an executable program, a umask value of 027 results in all permissions for the owner, read and execute permissions for members of the same group, and no permissions for all others. |
Environment Variables | |
setenv PATH /usr/bin:/usr/local/bin: | Specifies the search path. In this case, /usr/bin is searched first, and /usr/local/bin is searched second. |
set cdpath=.:..:$HOME | cdpath is a variable that sets the search path for the cd command. This variable assignment specifies that the cd command should search for the named directory in the current directory (.) first, in the parent directory (..) second, and the home directory ($HOME) third. |
setenv EDITOR emacs | Specifies the emacs editor as the default editor when running a program that allows you to edit a file. For example, various mail programs allow you to use an editor to compose and edit messages. |
setenv MAILHOST boston | Specifies boston as your mail handling system. |
setenv PRINTER sales | Specifies the printer sales as your default printer. |
Table 8-4 describes C shell metacharacters (characters that have special meaning to the shell).
Metacharacter | Description |
Syntactic | |
; | Separates commands that should be executed sequentially. |
| | 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. |
( \) | Groups commands to run as a separate process in a subshell. |
& | Runs commands in the background. |
Filename | |
/ | Separates the parts of a file's pathname. |
? | Matches any single character except a leading dot (.). |
* | Matches any sequence of characters except a leading dot (.). |
[ ] | Matches any of the enclosed characters. |
\~ | Specifies a home directory when used at the beginning of filenames. |
Quotation | |
'...' | Specifies that any of the enclosed characters 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), and \ (backslash) 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. |
Input/Output | |
< | 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. |
>>& | Redirects both diagnostic and standard output to the end of an existing file. |
>! | Redirects ouput and specifies that if the noclobber variable is set (prevents overwriting of files); it should be ignored so that the file can be overwritten. |
Substitution | |
$ | Specifies variable substitution. |
! | Specifies history substitution. |
: | Precedes substitution modifiers. |
^ | Used in special kinds of history substitution. |
` | Specifies command substitution. |
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.
You may want to enter the following three commands in your .cshrc file:
Creates a history buffer that stores the command lines you enter. The n entry specifies the number of command lines you want to store in the history buffer.
Saves the command lines you entered during the current login session and makes them available for the next login session. The n entry specifies the number of command lines you want to store in the history buffer when you log out.
Causes your C shell prompt to display the number of each command line.
To see the contents of the history buffer, use the history command. The displayed output will be similar to the following (your output will vary):
[18] %
history
3 set history=15
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
18 history
[19] % _
To reexecute any command in the command history buffer, use the commands listed in Table 8-5. Each command starts with an exclamation point (!), which tells the C shell that you are using commands in the history buffer.
Command | Description |
!! | Reexecutes the previous command. |
!n | Reexecutes the command specified by n. For example, using the history buffer shown in the previous display, !5 reexecutes the cd /usr/sales command. |
!-n | Reexecutes a previous command relative to the current command. For example, using the history buffer shown in the previous display, !-2 invokes command number 17, vi status. |
!string | Reexecutes the most recent command that has first characters matching those specified by string. For example, using the history buffer shown in the previous display, !cp invokes command number 7, cp report report5. |
!?string | Reexecutes the most recent command line that has any characters matching those specified by string. For example, using the history buffer shown in the previous display, !?Q1 invokes command number 14, cd /usr/sales/Q1. |
The command history buffer also allows you to reuse previous command arguments as well as to modify previous command lines. For information on these features, see the csh(1) reference page.
The C shell allows you to enter a portion of a filename or pathname at the shell prompt, and the shell will automatically match and complete the name. This feature saves you time when you are trying to display long, unique filenames.
For example, assume that you have the file meetings_sales_status in your current directory. To display a long listing of the file, enter the following command:
%
ls -l meetings[Escape]
The system displays the following on the same command line:
%
ls -l meetings_sales_status
You can now execute the command by pressing Return.
For more detailed information on filename completion, see the csh(1) reference page.
The command aliases feature allows you to abbreviate long command lines or rename commands. You do this by creating aliases for long command lines that you frequently use.
For example, assume that you often need to move to the directory /usr/chang/reports/status. You can create an alias status, which will move you to that directory whenever you enter it on the command line.
In addition, aliases allow you to make up more descriptive names for commands. For example, you could define an alias named rename for the mv command.
To create aliases, use the alias command. The format of the alias command is:
alias aliasname command
The aliasname entry specifies the name you want to use. The command entry specifies either the original command or a series of commands. If the command has more than one part (has spaces), enclose the whole expression in single quotes (' '). For example, to create the alias status that moves you to the directory /usr/chang/reports/status, enter the following command:
%
alias status 'cd /usr/chang/reports/status'
The usual way to define aliases is to make them a permanent part of your environment by including them in your .cshrc file. As a result, you can use the aliases whenever you log in or start a new shell. See Section 8.2.1 for an example.
To display all alias definitions, enter the following command:
%
alias
To display the definition of a particular alias, enter the following command:
%
alias
aliasname
The aliasname entry specifies the particular alias for which you are requesting a definition.
To remove an alias for the current login session, use the unalias command. The general format of the unalias command is the following:
unalias aliasname
The aliasname entry specifies the alias you want to remove.
To remove an alias for the current and all future login sessions, do the following:
%
unalias
aliasname
The aliasname entry specifies the alias you want to remove.
%
source .cshrc
For complete information on using aliases with the C shell, see the csh(1) reference page.
The C shell provides variables that can be assigned values. These variables can be very useful for storing values that can be later used in commands. In addition, you can directly affect shell behavior by setting those variables to which the shell itself refers.
Table 8-6 describes selected C shell built-in variables that are of the most interest to general users. For a complete list of C shell built-in variables, see the csh(1) reference page.
Variable | Description |
argv | Contains a value or values that can be used by the shell or shell scripts. |
cwd | Contains the pathname to your current directory. The value of this variable changes every time you use the cd command. |
home | Contains the pathname of your home directory. The default value for this variable is specified in the /etc/passwd file. |
ignoreeof | Specifies whether Ctrl/D can be used to log out from the system. If set, you must use either logout or exit to log out. If unset, you may use Ctrl/D to log out. This variable is usually set in the .cshrc file. |
cdpath | Specifies alternative directories to be searched by the system when locating subdirectories with the cd, chdir, or pushd commands. This variable is usually set in the .login file. |
noclobber | Specifies whether a file can be overwritten. If set, places restrictions on output redirection > to ensure that files are not accidentally destroyed, and that >> redirections refer to existing files. If set, a file cannot be overwritten. This variable is usually set in the .cshrc file. |
notify | Specifies whether you want to be notified when a background process has completed. If set, you are notified; if unset, you are not notified. This variable is usually set in the .cshrc file. |
path | Specifies the search path that the shell uses to find commands. This variable is usually set in the .login file. |
prompt | Can be used to customize your C shell prompt. This variable is usually set in the .cshrc file. |
shell | Specifies the shell to create when a program creates a subshell. This variable is usually set in the .login file. |
status | Specifies whether the most recently executed command completed without error (a value of zero is returned) or with an error (a nonzero value is returned). |
Table 8-7 describes selected C shell commands that are of the most interest to general users. For a complete list of C shell built-in commands, see the csh(1) reference page.
Command | Description |
alias |
Assigns and displays alias definitions. |
bg |
Puts a suspended process in the background. |
echo | Writes arguments to the shell's standard output. For more information and the command format, see the csh(1) reference page. |
fg |
Puts a currently running background process in the foreground. |
history |
Displays the contents of the command history buffer. |
jobs |
Displays the job number and the PID number of current
background processes. |
logout | Terminates the login session. |
rehash | Tells the shell to recompute the hash table of command locations. Use this command if you add a command to a directory in the shell's search path and want the shell to be able to find it. If you do not use rehash, the command cannot be executed because it was not in the directory when the hash table was originally created. |
repeat | Repeats a command a specified number of times. For more information and the command format, see the csh(1) reference page. |
set |
Assigns and displays shell variable values. |
setenv |
Assigns environment variable values. |
source |
Executes commands in a file. This can be used to update the current
shell environment. |
time | Displays the execution time of a specified command. For more information, see the csh(1) reference page. |
unalias |
Removes alias definitions. |
unset |
Removes values that have been assigned to variables. |
unsetenv |
Removes values that have been assigned to environment variables. |
Table notes:
This section describes the following Bourne shell features:
If your login shell is the Bourne shell, the operating system executes the .profile login script to set up your environment.
The .profile login script variables that are exported are passed to any subshells and subprocesses that are created. Variables that are not exported are used only by the login shell.
In the following .profile login script, shell variables are set and exported, a trap is set for the logout script, and the system is instructed to display information. Table 8-8 explains each part of the script.
# Set PATH PATH=/usr/bin:/usr/local/bin: # Export global variables export PATH # Set shell variables PS1='$LOGNAME $ ' CDPATH=.:..:$HOME # Set up for logout script trap "echo logout; $HOME/.logout" 0 # Display status information date echo "Currently logged in users:" ; users
Command | Description |
Set Search Path | |
PATH=/usr/bin:/usr/local/bin: | Specifies the search path. In this case, /usr/bin is searched first and /usr/local/bin searched second. |
Export Search Path | |
export PATH | Specifies that the search path is to be passed to all commands that you execute. |
Set Shell Variables | |
PS1='$LOGNAME $ ' | PS1 is the variable that specifies the Bourne shell prompt, and its default value is $. However, this variable assignment specifies that your prompt should be changed to the following: username $. For example, if your username were amy, your prompt would be the following: amy $. |
CDPATH=.:..:$HOME | CDPATH is a variable that sets the search path for the cd command. This variable assignment specifies that the cd command should search for the named directory in the current directory (.) first, in the parent directory (..) second, and the home directory ($HOME) third. |
Set Up Logout Script | |
trap "echo logout; $HOME/.logout" 0 |
Specifies that your shell should display
logout
and execute
your
.logout
script when the
trap
command captures the
exit signal (0). |
Display Status Information | |
date | Displays the date and time. |
Table notes:
Table 8-9 describes Bourne shell metacharacters (characters that have special meaning to the shell).
Metacharacter | Description |
Syntactic | |
| | Separates commands that are part of a pipeline. |
&& | Runs the next command if 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 to run as a separate process in a subshell. |
Filename | |
/ | Separates the parts of a file's pathname. |
? | Matches any single character except a leading dot (.). |
* | Matches any sequence of characters except a leading dot (.). |
[ ] | Matches any of the enclosed characters. |
Quotation | |
\ | 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 ' quote character ) 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), and \ (backslash) 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. |
Input/Output | |
< | 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. |
2> | Redirects diagnostic output to a specified file. |
Substitution | |
${...} | Specifies variable substitution. |
`...` | Specifies command output substitution. |
The Bourne shell provides variables that can be assigned values. The shell sets some of these variables, and you can set or reset all of them.
Table 8-10 describes selected Bourne shell built-in variables that are of most interest to general users. For complete information on all Bourne Shell built-in variables, see the sh(1) reference page.
Variable | Description |
HOME | Specifies the name of your login directory, the directory that becomes the current directory upon completion of a login. The cd command uses the value of HOME as its default value. HOME is set by the login command. |
PATH | Specifies the directories through which your system should search to find and execute commands. The shell searches these directories in the order specified here. Usually, PATH is set in the .profile file. |
CDPATH | Specifies the directories that the cd command will search to find the specified argument to cd. If cd's argument is null, or if it begins with a slash (/), dot (.), or dot dot (..), then CDPATH is ignored. Usually, CDPATH is set in your .profile file. |
The pathname of the file where your mail is deposited. You must set MAIL, and this is usually done in your .profile file. | |
MAILCHECK | Specifies in seconds how often the shell checks for mail (600 seconds is the default). If the value of this variable is set to 0, the shell checks for mail before displaying each prompt. MAILCHECK is usually set in your .profile file. |
SHELL | Specifies your default shell. This variable should be set and exported by your .profile file. |
PS1 | Specifies the default Bourne shell prompt, and its default value is $. PS1 is usually set in your .profile file. If PS1 is not set, the shell uses the standard primary prompt string. |
PS2 | Specifies the secondary prompt string - the string that the shell displays when it requires more input after you enter a command line. The standard secondary prompt string is a > symbol followed by a space. PS2 is usually set in your .profile file. If PS2 is not set, the shell uses the standard secondary prompt string. |
Table 8-11 describes selected Bourne shell commands that are of the most interest to general users. For a complete list of Bourne shell built-in commands, see the sh(1) reference page.
Command | Description |
cd |
Allows you to change directories. If no directory is specified, the
value of the
HOME
shell variable is used. The
CDPATH
shell
variable defines the search path for this command. |
echo |
Writes arguments to the standard output. |
export | Marks the specified variable for automatic export to the environments of subsequently executed commands. |
pwd |
Displays the current directory. |
set |
Assigns and displays variable values. |
times | Displays the accumulated user and system times for processes run from the shell. |
trap |
Runs a specified command when the shell receives a specified signal. |
umask |
Specifies the permissions to be subtracted
for all new files created. |
unset |
Removes values that have been assigned to variables. |
Table notes:
This section describes the following Korn shell features:
If your login shell is the Korn shell, the operating system processes the .profile login script in your home directory. The .profile login script defines environment variables. These variables are used by your login shell as well as any subshells and subprocess that are created. The .profile login 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 .kshrc file in your home directory, make sure that the ENV=$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 .profile login 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
Command | Description |
Set Environment Variables | |
PATH=/usr/bin:/usr/local/bin | Specifies the search path. In this case, /usr/bin is searched first and /usr/local/bin searched second. |
ENV=$HOME/.kshrc | Specifies $HOME/.kshrc as the login script. |
EDITOR=vi | Specifies vi as the default editor for command line editing at the shell prompt and for filename completion. |
FCEDIT=vi |
Specifies
vi
as the default editor for the
fc
command. |
PS1="`hostname` [!] $ " | PS1 is 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 hostname command, followed by the command number of the current command, followed by the dollar sign ($). For example, if the name of your system is boston, and the current command is numbered 30, your prompt would be the following: boston[30] $. |
Export Global Variables | |
export PATH ENV EDITOR FCEDIT PS1 | Specifies that the values of the PATH, ENV, EDITOR, FCEDIT, and PS1 variables should be exported to all subshells. |
Set Mail Variables | |
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/mail directory. |
MAILCHECK=300 | Specifies that the shell should check for mail every 300 seconds (5 minutes). |
Table notes:
In the following .kshrc login 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
Command | Description |
Shell Variables | |
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. |
Command Aliases | |
alias rm='rm -i' | Specifies the use of the -i option (which prompts you for file deletion) with the rm command. |
alias rename='mv' | Specifies rename as a new name for the mv command. |
alias h='history' \!* | more' | Defines a command that pipes the contents of the command history buffer through the more command. The \!* string specifies that all of the history buffer should be piped. |
alias l='ls -l' | Defines a short name for the ls -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. |
History Variables | |
HISTSIZE=40 | Instructs the shell to store the last 40 commands in the history buffer. |
Set File Creation Permissions | |
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. |
Table 8-14 describes Korn shell metacharacters (characters that have special meaning to the shell).
Metacharacter | Description |
Syntactic | |
| | 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. |
Filename | |
/ | 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. |
Quotation | |
\ | 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. |
Input/Output | |
< | 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. |
Substitution | |
${...} | Specifies variable substitution. |
% | Specifies job number substitution. |
`...` | Specifies command output substitution. |
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 the history command. 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.
Command | Description |
r | Reexecutes the previous command |
r n | Reexecutes the command specified by n. For example, using the history buffer shown in the previous display, r 5 reexecutes the cd /usr/sales command. |
r -n | Reexecutes a previous command relative to the current command. For example, using the history buffer shown in the previous display, r-2 invokes command number 16, cd /usr/chang. |
r string | Reexecutes the most recent command that has first characters matching those specified by string. For example, using the history buffer shown in the previous display, r cp invokes command number 7, cp report report5. |
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 the HISTSIZE variable in your .profile file. This variable has the following format:
HISTSIZE=n
The n entry 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 as vi or emacs. For information on these features, see the following section.
The Korn shell allows you to list and/or edit the command lines in your command history buffer. As a result, you may modify any element of a previous command line and then reexecute the command line.
The command line editing functions for the Korn shell are extensive. This section covers only the most basic functions. For more detailed information, see the ksh(1) reference page.
To display the command history buffer and/or to edit its contents, use the built-in command fc (fix command). The fc command has the following two formats:
fc [ -e editor ] [ -nlr ] [ first ] [ last ]
This command format allows you to display and/or edit any number of command lines in your buffer.
If you want to specify a default editor for the -e flag, define the FCEDIT variable in your .profile script. For example, if you want to make emacs your default editor, enter the following variable definition:
FCEDIT=emacs
fc -e - [ old=new ] [ string ]
This command allows you to immediately replace an old string with a new string within any previous command line.
The following section contains some examples of fc use.
The Korn shell also allows you to edit individual command lines at the shell prompt by using a command set similar to the vi or the emacs editors. For more information on this feature, see the ksh(1) reference page.
Example 1: Displaying Command Lines in the Command History Buffer
To display command lines 15 to 18, enter the following command:
$
fc -l 15 18
You may also list the same command lines by specifying command strings
instead of line numbers, as in the following example:
$
fc -l ls more
Example 2: Editing and Executing Command Lines
To display and edit command lines 15 to 18 with the
vi
editor,
enter the following command:
$
fc -e vi 15 18
After making your edits, write and exit the file with the
:wq!
command.
The command lines in the file are then reexecuted.
Example 3: Replacing and Reexecuting Command Lines
Assume that you have just entered the
echo hello
command, and
now want to replace
hello
with
goodbye.
To do the
replacement and reexecute the command line, enter the following command:
$
echo hello
For more detailed information on the
fc
command and command line editing,
see the
ksh(1)
reference page.
The Korn shell allows you to enter a portion of a filename or pathname
at the shell prompt and the
shell will automatically match and complete the name. If there
is more than one filename or pathname that matches the criterion,
the shell will provide you with a list of possible matches.
To activate the filename completion mechanism, define the
EDITOR
variable in your
.profile
file. For example, if you
want to use the
vi
editor, enter the following variable
definition in your
.profile
file:
EDITOR=vi
To demonstrate how filename completion works, assume that your
editor is
vi
and that you
have the
salesreport1,
salesreport2,
and
salesreport3
files in your current directory.
To display a long listing and to
activate filename completion, enter the following command:
$
ls -l salesreport[Escape]=
The system redisplays your command, and the cursor is
now at the end of
salesreport.
If you want to choose
salesreportjan,
type
a
(the
vi
append
command) followed by
jan,
then press
Return.
The listing
for
salesreportjan
will be displayed.
For more detailed information on filename completion, see
the
ksh(1)
reference page.
The command aliases feature allows you to abbreviate long command
lines or rename commands. You
do this by creating
aliases
for long command lines that
you frequently use.
For example, assume that you often need to move to
the directory
/usr/chang/reports/status.
You can create an alias
status,
which will move you to that directory whenever you enter it
on the command line.
In addition, aliases allow you to make up more descriptive names for
commands. For example, you could define an alias named
rename
for the
mv
command.
To create aliases, use the
alias
command. The general format
of the
alias
command is the following:
alias
aliasname=command
The
aliasname
entry specifies the name you want to use. The
command
entry specifies either the original command or a series
of commands. If the
command
has more than one part (has spaces),
enclose the whole expression in single quotes.
For example, to create the alias
status
that moves you to the
directory
/usr/chang/reports/status,
enter the following command:
alias status='cd /usr/chang/reports/status'
The usual way to define aliases is to place them in your
.kshrc
file so that you can use them whenever you log in or start a new shell.
See
Section 8.4.1
for an example.
To display all alias definitions, enter the following command:
$
alias
To display the definition of a particular alias, enter the following
command:
$
alias
aliasname
The
aliasname
entry specifies the particular alias for which you
are requesting a definition.
The Korn shell allows you to export the aliases you create.
Variables that are exported are passed
to any subshells that are created so that when
you execute a shell procedure or new shell, the alias remains defined.
(Variables that are not exported are
used only by the login shell.)
To export an alias, use the following form of the
alias
command:
alias -x
aliasname=command
The
-x
flag specifies that you want to export the alias.
The
aliasname
entry specifies the name you want to use. The
command
entry specifies either the original command or a series
of commands. If the
command
has more than one part, enclose the
whole expression in single quotes.
For example, to export an alias definition for the
rm
command, enter the following:
alias -x rm='rm -i '
You can enter the preceding command in one of two ways:
To remove an alias for the current login session,
use the
unalias
command. The general format
of the
unalias
command is the following:
unalias
aliasname
The
aliasname
entry specifies the alias you want to remove.
To remove an alias for the current and all future login sessions,
do the following:
$
unalias
aliasname
The
aliasname
entry specifies the alias you want to remove.
$
source .kshrc
The Korn shell provides additional aliasing features.
For complete information on using aliases with the Korn
shell, see the
ksh(1)
reference page.
The Korn shell provides variables that can be assigned values. The shell
sets some of these variables, and you can set or reset all of them.
Table 8-16
describes selected Korn shell built-in variables that are
of the most interest to general users. For complete information on all Korn
shell built-in variables, see the
ksh(1)
reference page.
Table 8-17
describes selected Korn shell commands that are of
the most interest to
general users. For a complete list of Korn shell built-in commands,
see the
ksh(1)
reference page.
Table notes:
15 ls -la
16 pwd
17 cd /u/ben/reports
18 more sales
$
15 ls -la
16 pwd
17 cd /u/ben/reports
18 more sales
$
ls -la
pwd
cd /u/ben/reports
more sales
~
~
~
~
hello
$
fc -e - hello=goodbye echo
echo goodbye
goodbye
8.4.5 Filename Completion
1) salesreportfeb
2) salesreportjan
3) salesreportmar
$ ls -l salesreport
8.4.6 Aliases
8.4.7 Built-In Variables
Table 8-16: Built-In Korn Shell Variables
Variable
Description
HOME
Specifies the name of your login directory.
The
cd
command uses the value of
HOME
as its default
value. In Korn shell procedures, use
HOME
to
avoid having to use full
pathnames - something that is especially helpful if the pathname
of your login directory changes.
HOME
is set
by the
login
command.
PATH
Specifies the directories through which your system should search to
find and execute commands. The shell searches these directories
in the order specified here. Usually,
PATH
is set in the
.profile
file.
CDPATH
Specifies the directories that the
cd
command will search
to find the specified argument to
cd.
If the
cd
argument is
null, or if it begins with a slash (/), dot (.), or dot dot
(..), then
CDPATH
is ignored.
Usually,
CDPATH
is set in your
.profile
file.
MAIL
The pathname of the file where your mail is deposited.
MAIL
is usually set in your
.profile
file.
MAILCHECK
Specifies in seconds how often the shell checks for mail (600 seconds
is the default). If the value of this variable is set to 0, the shell
checks for mail before displaying each prompt.
MAILCHECK
is usually set in your
.profile
file.
SHELL
Specifies your default shell. This variable should be
set and exported by your
.profile
file.
PS1
Specifies the default Korn shell prompt, and its default value is
the dollar sign ($).
PS1
is usually set in your
.profile
file.
If
PS1
is not set, the shell uses the standard primary prompt string.
PS2
Specifies the secondary prompt string - the
string that the shell displays when it requires more input after entering
a command line. The standard
secondary prompt string is
a
>
symbol followed by a space.
PS2
is usually set in your
.profile
file. If
PS2
is not set, the shell uses the
standard secondary prompt string.
HISTFILE
Specifies the pathname of the file that will be used to store
the command history. This variable is usually set in your
.profile
file.
EDITOR
Specifies the default editor for command line editing at the shell
prompt and for filename completion.
This variable is usually set in your
.profile
file.
FCEDIT
Specifies the default editor for the
fc
command. This variable is
usually set in your
.profile
file.
HISTSIZE
Specifies the number of previously entered commands that
are accessible by this shell. The default is 128. This variable is
usually set in your
.kshrc
file.
8.4.8 Built-In Commands
Table 8-17: Built-In Korn Shell Commands
Command
Description
alias
Assigns and displays alias definitions.
cd
Allows you to change directories. If no directory is specified, the
value of the
HOME
shell variable is used. The
CDPATH
shell
variable defines the search path for this command.
echo
Writes arguments to the standard output. For more information and the
command format, see the
ksh(1)
reference page.
export
Marks the specified variable for automatic export to the environments
of subsequently executed commands.
fc
Allows you to display, edit, and reexecute the
contents of the command history buffer.
history
Displays the contents of the command history buffer.
jobs
Displays the job number and the PID number of current background processes.
pwd
Displays the current directory.
set
Assigns and displays variable values.
times
Displays the accumulated user and system times for processes run from
the shell.
trap
Runs a specified command when the shell receives a specified signal.
umask
Specifies the permissions to be subtracted from
the default permissions set by the creating program
for all new files created.
unalias
Removes alias definitions.
unset
Removes values that have been assigned to variables.