previous
section   next section   upper level

Section 13: Interactive Use of the Shell

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

File name completion

Both csh and tcsh will perform file name completion for you. You can type in a partial file name, and press the ESCAPE key for csh, or the TAB key for tcsh. 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 tcsh 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 csh or tcsh shell

alias hi history

EXERCISE: Create an alias in the TC 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 dirvar=$old; cd $dirvar; unset dirvar'

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 dirvar. 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 dirvar, 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 TC 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.

csh, and tcsh
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. The dollar sign represents the last word in this case. 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:$

previous
section   next section   upper level