Friday, December 02, 2005

Remember the Pipe

Sometimes, you want to save output. For that, the shell gives you redirection.

Sometimes, though, you want to save the pipeline that you grew to get the
output.

You could recall it, write it down, and then type it into a file, but there's
an easier way.

After you run the pipeline, make your next command "fc."

Try this:
ls | wc -l
fc
(You've run other things since the pipeline? Recall it with command history, rerun it, then type fc)

Once fc puts the pipeline into a text editor, you can write it out to a file. Presto! A shell script.

And as long as you're in there, you could even take your time, add shebang lines and comments, and change the layout. The shell gives you command-line history and pipes, for quick-and-dirty script development, and then gives you fc, to capture what works and clean it up.

The default editor is vi, but you can make it whatever you want.
EDITOR=kate
ls | wc -l
fc
Notice that when you exit the editor, the command is rerun. fc stands for "fix command". Instead of editing a long command line with arrow keys, you can edit it with your favorite text editor.

Being able to write to a file and create a shell script, once you've edited it, is just a side-effect of the shell's clean design.

And now that we're talking about command history, we'll make that next week's introductory topic.

0 Comments:

Post a Comment

<< Home