P1: JDW
UNIX WL040/Bidgoli-Vol III-Ch-41 August 13, 2003 17:26 Char Count= 0
504 UNIXOPERATINGSYSTEMTable 1Command Line Shortcuts Available in Shells
Supporting Command History!:0 The previous command name
!:n Repeat argumentnof the
previous command
!:ˆ[or !ˆ, leaving out Repeat the first argument of the
the colon] previous command
!:$ [or !$, leaving out Repeat the last argument of the
the colon] previous command
!:n1–n2 Repeat the arguments between
n1andn2from the previous
command, inclusive
!:-n Repeat arguments 0 throughn
from the previous commandTable 1 lists some of the command line shortcuts that are
available in shells supporting command history.Command Line Editing
Most modern Unix shells (with the exception of the orig-
inal Bourne shell) support a feature known as command
line editing. Command line editing allows one to use the
history buffer to move back to previous commands and
modify them using a line editing syntax from Unix edi-
tors, namely, vi and emacs. The user can also edit current
command lines as he or she types. For example, in the
Korn shell, a user can tell the shell that he wants to edit
command lines as if he were in the emacs editor, by is-
suing the command “set -o emacs” (“set -o vi” would set
the editing for vi emulation). Once he has set emacs em-
ulation, the user can navigate the command history and
command line using familiar emacs cursor movement and
editing syntax.
For example, to go to the previous line in an emacs
buffer, one would press Ctrl-p. To go to the next (subse-
quent) line, one would press Ctrl-n. To move backward
on a line, character by character, one would press Ctrl-b
repeatedly. To move forward character by character, one
would press Ctrl-f. A user can walk backward character
by character on a command line, by pressing Ctrl-b. The
ability to search backward through the command history
is also available in many shells through the Ctrl-r key com-
bination. Table 2 shows some of the more common emacs
command line editing commands.Command Line Completion
Coupled with command line editing is command line
completion. Imagine that there are three files in the cur-
rent directory: file1000a, file2000b, and file2010c. A shell
that offers command line completion will allow the user
to type just enough of a filename and then press a com-
mand line completion key, and the shell will examine the
file system andcompletethe rest of the filename automa-
tically. For example, suppose a user is using the bash shell,
enters the command “ls file1,” and then presses the tab
key. The tab key is the completion key for bash and tcsh
(it’s ESC ESC in the Korn shell). The shell automatically
completes the filename, leaving you with “ls file1000a” on
the command line.Table 2Common Emacs Command Line Editing CommandsMeta-b Move one word back on the
command line (the Meta key
is often the “Alt” key on a PC
keyboard)
Meta-f Move one word forward on the
command line
Meta-backspace Delete the word directly before the
cursor’s current position on the
command line
Ctrl-a Move to the beginning of the
command line
Ctrl-e Move to the end of the command
line
Meta-d Delete the next word in the
command lineAliases
Sometimes users want shorter names for lengthy common
command lines. Suppose, for instance, that every time a
user wanted to run the emacs editor to edit a file, she
wanted to set the colors and font size. So for every file she
edits, she would need to type something like the following
line:/usr/bin/emacs -fn 6x13 -fg black -bg cyan
-cr blue -ms white filename.txtThe user would certainly get tired of typing that long
command every time she wanted to edit a file. Aliases can
be used to store commands for later use. For example, one
could store the core of the previous command (minus the
filename) in an alias, and then simply use the alias to refer
to the longer command:alias myemacs = "/usr/bin/emacs -fn 6x13 -
fg black -bg cyan -cr blue -ms white"Once that command is issued, the user could then sim-
ply type “myemacs filename.txt,” and the myemacs alias
would expand into the longer command line associated
with the alias, setting all her desired colors and the font
size to 6×13. Aliases, and other shell commands (such as
the set -o emacs command) are generally placed in startup
command files, which are default shell scripts loaded by
the shell each time a user logs in. In bash, the main file is
.bashrc. In the Korn shell, it is.kshrc. For the C Shell, it is
.cshrc (.tcshrc for the TC shell).Shell Variables
Shells also allow the creation of user-defined variables that
enable the shell programmer to store temporary values. A
shell variable is usually an all capital letter name refer-
encing a particular value. Shell variables are accessed by
placing a dollar sign in front of the variable name. For ex-
ample, one of the standard shell variables is called $USER,
which contains the username of the user currently logged
into a given terminal. The echo command will print out
to the terminal the value of any variable. So, issuing the