[Chapter 9] 9.25 The C Shell repeat Command
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:35:34Z

#

| Chapter 9 Saving Time on the Command Line |

## 9.25 The C Shell repeat Command

The C shell has a built-in command that lets you execute a command repeatedly:

>     % repeat n command

All you do is specify the number of repetitions, followed by the command you want to re-execute. A trivial example would be:

>     % repeat 4 echo Enter name:
>     Enter name:
>     Enter name:
>     Enter name:
>     Enter name:

Simple, right? Just imagine what Jack Nicholson could have done in the movie The Shining if he had traded in his typewriter for a UNIX system:

>     % repeat 500 echo "All work and no play makes Jack a dull boy."

Ok, this is fun at first, but you may soon wonder whether this command has any down-to-earth uses. It does, and I'll conclude with some more useful examples:

1. Print three copies of memo:

   >     % repeat 3 pr memo | lp

2. Run popd (14.6) four times to clear a directory stack:

   >     % repeat 4 popd

3. Append 50 boilerplate files to report:

   >     % repeat 50 cat template >> report

Some versions of the C shell repeat command have a quoting bug. See the end of article 47.2.

- DG

9.24 Get File List by Editing Output of ls -l, grep, etc. |  | 9.26 Expect