EXAMPLE: To alias the "history" command to "hi" you could use the following command in the csh or tcsh shell
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
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 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:
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
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. The dollar sign represents the last word in this case. Here are some examples: