C Shell Command Recall

Redoing commands in C Shell

The C Shell provides a "history" mechanism which keeps track of the commands
that you executed and allows you to recall those commands, to help reduce your
typing.  In order to use it, you must first "turn it on."  This is usually done
in your .cshrc file with the command

set history=100

This will cause the shell to remember the last 100 commands that you executed.
You will also find it useful to save those commands after you log out.  To do
this put the following in your .cshrc file.

set savehist=100

To list the commands that have been "remembered" you use the "history" command.

bugs> history
     1  ls
     2  ls -la .forward
     3  cat .forward
     4  vi .forward
     5  more .cshrc
     6  grep hist .cshrc .login
     7  history

Since "history" is a rather long command to type, and it displays up to 100
(cause that's what we set it for) commands, most folks set up an alias such 
that "h" will display the commands one page at a time.

alias h   'history \!* | more'

You can also use "h n" (where n is some number between 0 and 100).  This will
display the last n commands.  For example to display the last 12 commands you
would

h 12

Now, to recall a command you have 2 choices.  You can use the "command number"
as listed above, or the command.  So, to recall and execute command number 4
(vi .forward) you would do one of the following

!4
!vi

Likewise, to reexecute the grep command (number 6 above), do one of these.

!6
!grep

You don't have to enter the whole command name, just enough to make it unique
looking backwards through the list.  Assume your history list looks like this.

bugs> history
     1  cat .forward
     2  vi .forward
     3  more .cshrc
     4  grep hist .cshrc .login
     5  cd /

Now, you want to reexecute command number 1 (cat .forward).  You would do one
of the following.

!1
!ca
!cat

You wouldn't want to "!c" cause that would reexecute command number 5 (cd /).

You do have the ability to execute a previous command with a minor change.
Assume you want to reexecute command number 4, but you want to change "hist"
to "path."

!g:s/hist/path/

This says to reexecute the last command that started with a "g" and replace
hist with path.

Now you might want to use "ls" on those two files.  You can

ls -l !g:2-3

This will run the ls command and grab the 2nd and 3rd arguments from the
previous grep command.  So, the command that gets executed is 
"ls -l .cshrc .login"

And there's more.  There is a special mechanism for doing things with the
very last command that you executed.  Assume you did the following

grep bunny friends

and the list was very long.  You would like to pipe it to "more," so you do
the following.

!! | more

The "!!" says to rexecute the previous command.  

Here's something I like to do when I need to modify a file to test something,
but I don't want the date on the original to be changed.

mv file1.c file1.c.orig
cp !$ !^
vi !$

The "!$" grabs the last argument (to the previous command) and the "!^" grabs
the first argument.  You could also use "!:2" for the 2nd argument, and "!:1" 
for the first argument.  Using that style you can pluck off any argument you
like from a previous command.
Here's the equivalent commands.
mv file1.c file1.c.orig
cp file1.c.orig file1.c
vi file1.c

Oh, one other thing we'll cover.  To make a quick change in a previous command
you can use "^"  That is the "hat" or "carat" character.  Assume you entered
the following

sed "s/Bugs/Tweety/" myflie > output

you wanted to enter myfile, but entered myflie by mistake.  To fix it you can

^li^il

This will replace the first occurence of the string "li" with "il" thus 
executing

sed "s/Bugs/Tweety/" myfile > output

Well, I could go on, but this seems like enough for now.

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.