Interactive Use of the Unix Shell

Section 13: Interactive Use of the Shell

This section discusses tips and tricks to make your use of the shellmore efficient.

File name completion

Both ksh and csh will perform file name completion for you. You cantype in a partial file name, and press the ESCAPE key (once for csh,twice for ksh). The shell will then complete the name of the file foryou. If no file exists that begins with the characters you typed, theshell will beep at you, and will not complete the name. If more thanone file begins with the characters you typed, the shell will completethe name up to the point where the names differ. Then you can typeadditional letters to specify the file name you want, reusing theESCAPE key if desired.

Command name aliasing

Both csh and ksh provide command name aliasing, to allow you to renamecommands. Aliasing can save a lot of keystrokes if you mustfrequently issue the same lengthy command. The alias command requirestwo pieces of information: The command you wish to alias, and thealias you wish to use to refer to it.

EXAMPLE: To alias the "history" command to "hi" you could usethe following command in the Korn shell:

aliashi='history'

After entering that alias, you could type thecommand "hi" and the shell would substitute "hi" with the string"history" before executing it. The same command could be accomplishedin the C shell with the syntax:

alias hi history

EXERCISE: Create an alias in the Korn shell called "clean" thatwould remove any files from your home directory that have theextension .gif or .jpg.

EXPLANATION: The command

alias clean='rm ~/*.gif; rm~/*.jpg'

would work.

Command aliasing can be tricky. Surround the alias string with singlequotes (') to prevent the shell from interpreting special characters.If you use command history substitution in an alias, use the backslashcharacter (\) to escape characters that you don't want the shell tointerpret.

EXAMPLE: This example, written for the C shell, creates analias for the cd command, so that it stores the current location in ashell variable called old before it changes to the new location. Italso creates a new command alias called back that allows us to go backto the previous location:

There are several things to note in the above example. The alias forcd has three parts: The first reads the current working directory fromthe shell variable cwd, and saves it in a shell variable called old.The second part uses history substitution and chdir to change thecurrent location. The use of chdir prevents an "aliasing loop," wherethe cd command calls itself. The third part executes the pwd commandto print the new location on the screen.

The alias for back also has three parts: The first part reads theprevious location from the shell variable old, and stores it in ashell variable called foo. That is necessary because the new cd aliaswill change the value of old when we call it in the second part of theback alias. The third part cleans up our mess by unsetting thevariable foo, removing it from the environment.

You can remove an alias using the unalias command. To remove the"clean" alias you created in a previous exercise, enter the command:

unalias clean

Command history substitution

The C shell and Korn shell will keep an ordered list of the commandsyou have issued, and allow you to retrieve commands from the list.That facility, called command history substitution, makes itpossible to reuse all or part of your previously issued commands.Each command on the list is given a command number, according to theorder it was issued. You can view the command history list by issuingthe command:

history

The exact mechanism ofretrieving commands from the command history list depends on the shellyou're using, and how you have customized your shell.

ksh

When using the Korn shell, the number of commands remembered by theshell is controlled by the HISTSIZE environment variable. Use thecommand

HISTSIZE=50;export HISTSIZE

to set thelength of the history list to fifty. By default, the history size isset to 128 lines.

The shell command "set -o" is used to specify the editing mode for thecommand line, either emacs or vi. Since an earlier section of thisworkshop dealt with emacs, we will confine our discussion to the emacsediting style. To use the emacs editing mode, enter the command

set -o emacs

In emacs editing mode, recall previous commands with the emacs commandfor "previous line," or Control-P. Repeated use of Control-P willrecall earlier commands. You can also use the emacs command for "nextline," or Control-N, to go forward through your command history,toward more recently-issued commands. You can only use Control-Nafter you have used Control-P at least once.

csh

The C shell allows you to recall previous commands in whole or inpart. In the C shell, the history shell variable is used to specifythe number of lines the shell will remember. The statement

set history=60

will cause the C shell toremember sixty commands.

To recall previous commands from the history list, the C shell usesthe exclamation point (!) character, sometimes referred to in computerjargon as "bang." The bang character can be used in combination withhistory line numbers, and text patterns. Here are some examples ofhow to use history substitution in the C shell:

Recall the last command:

!!

Recall the third most recent command:

!-3

Recall command number ten from the history list:

!10

Recall the last command that began with the letters "ls":

!ls

You can also recall specific pieces of previous commands, and use themto create new commands. The colon character is used to selectspecific words from a command. Each word in the command is referredto by position. The command name itself is item number zero. Hereare some examples:

Recall the third word from the last command:

!:2

Perform an "ls" on the second word from command number 8:

ls !8:1

Use more to view the last item from command number ten:

more !10:$

Editing the command line

The Korn shell (ksh) provides the ability to edit the command historylist almost as if your were in an editor program like vi, or emacs.The current command is always the last line in the history list, andbegins blank. You can type in a new command, or recall an earliercommand from the list. You can also modify the text on the currentline using basic text editor commands.

The mechanism for setting the editor style in ksh is the "set -o"command. To edit in emacs mode, issue the command:

set -oemacs

Manipulating command line text in emacs mode is done in much the sameway as text editing with emacs. When you finish editing the commandline, press the return key to issue the command to the shell. Youmight want to go back and refresh your memory on emacs by reviewingsection ten of this tutorial, titled "Text Editing with Emacs."