Part 13. Interactive Use of the Shell

Written by Pantas Manik on 3:24 PM

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

File name completion

Both ksh and csh will perform file name completion for you. You can type 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 for you. If no file exists that begins with the characters you typed, the shell will beep at you, and will not complete the name. If more than one file begins with the characters you typed, the shell will
complete the name up to the point where the names differ. Then you can type additional letters to specify the file name you want, reusing the ESCAPE key if desired.

Command name aliasing

Both csh and ksh provide command name aliasing, to allow you to rename commands. Aliasing can save a lot of keystrokes if you must frequently issue the same lengthy command. The alias command requires two pieces of information: The command you wish to alias, and the alias you wish to use to refer to it.

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

alias hi='history'

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

alias hi history

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

EXPLANATION: The command:

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

would work.

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

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

alias cd 'set old=$cwd; chdir \!*; pwd'

alias back 'set foo=$old; cd $foo; unset foo'

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

The alias for back also has three parts: The first part reads the previous location from the shell variable old, and stores it in a shell variable called foo. That is necessary because the new cd alias will change the value of old when we call it in the second part of the back alias. The third part cleans up our mess by unsetting the variable 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 commands you have issued, and allow you to retrieve commands from the list. That facility, called command history substitution, makes it possible to reuse all or part of your previously issued commands. Each command on the list is given a command number, according to the order it was issued. You can view the command history list by
issuing the command:

history

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

ksh

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

HISTSIZE=50;export HISTSIZE

to set the length of the history list to fifty. By default, the history size is set to 128 lines.

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

set -o emacs

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

csh

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

The statement

set history=60

will cause the C shell to remember sixty commands.

To recall previous commands from the history list, the C shell uses the exclamation point (!) character, sometimes referred to in computer jargon as "bang." The bang character can be used in combination with history line numbers, and text patterns.

Here are some examples of how 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 them to create new commands. The colon character is used to select specific words from a command. Each word in the command is referred to by position. The command name itself is item number zero. Here are 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 history list 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, and begins blank. You can type in a new command, or recall an earlier command from the list. You can also modify the text on the current line 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 -o emacs

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

Related Posts by Categories



Widget by Hoctro | Jack Book
  1. 0 comments: Responses to “ Part 13. Interactive Use of the Shell ”