!!
)
and hitting return. Similarly, you can refer to the last token of a
command (usually, but not always, the filename) with the symbols !$
.
Another especially useful
form of history substitution uses the caret (^
, Shift-6 on
most keyboards) to allow you to modify parts of the last command you typed.
For example, suppose you are trying to edit a file called my.first.hw, and you accidentally type my.farst.hw. The following transcript shows how to use the caret to quickly make the change. The commands you type are shown in regular type; the computer's prompts and responses are shown in bold type.
springer.s243% emacs my.farst.hw
springer.s243% ^far^fir^
emacs my.first.hw
In other words, you surround the text to be replaced with carets, follow that with the text to be used in the replacement, and end it all with a final caret. The shell will echo the modified command.
!$
. To do so, follow the
variable or
modifier with a colon (:
) and one of the letters h (to extract
the head of the file, which is the leading part of the pathname), t
(to extract the tail of the file), r (to extract the root of the file,
which is the name minus any extension), or e (to extract the extension
of the file). These modifiers are especially useful when you use loops in
the shell (See part 4).
For example, while filename wildcards can be used to do lots of repetitive tasks (like copying or moving many files from one directory to another), there are some tasks (like creating links or renaming files) for which wildcards will rarely, if ever, do the right thing. While the syntax for the C shell's foreach loop is a bit odd, it nevertheless is very useful, especially combined with the modifiers described in part 3.
Suppose we wish to rename a large number of files which have the extension .for to have the same root name, but the extension .f. There is no solution using wildcards which can achieve this goal. The following transcript shows how a C shell foreach loop can do the job
springer.s243% foreach file (*.for)
? mv $file $file:r.f
? end
The shell uses a question mark ? as a prompt for the statements of the foreach loop. The variable $file takes on, in turn, each of the elements resulting in the wildcard expansion of *.for (which must be parenthesized), for each statement between the foreach and the end. Note that you must refer to the variable without a dollar sign in the foreach statement, but with a dollar sign in the statements inside the loop.
To have an alias (or any other C shell command) execute everytime you start up a shell, you can place the command in a file called .cshrc in your home directory. If you want to know more about this file, type help dotlogin.
Finally, if you want to override an alias, type the command name with
a leading backslash (\
). So to erase all your files without
being asked, you could type \rm *
instead of rm *
.