EXAMPLE: To alias the "history" command to "hi" you could use the following command in the Korn shell:
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
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:
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:
When using the Korn shell, the number of commands remembered by the shell is controlled by the HISTSIZE environment variable. Use the command
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
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.
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
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:
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:
The mechanism for setting the editor style in ksh is the "set -o" command. To edit in emacs mode, issue the command:
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."