[Bash Tricks]: Re-run a range of ‘history’ commands

Sometime we may want to run a range of commands which was run in the past (captured in ‘history‘ command). This can be achieved using ‘fc’ command.

Run ‘history’ command to find the range you want to re-run. For example, range from 1000 to 1010.

fc 1000 1010

The above command will copy the range of commands to a temporary file (same can be saved as shell script if you wish to re-run in future) and open it. The default editor is ‘vim’, use ‘:w [filename]’ to save to new file.

Running a single command from history can be done using ![command number in history]. For example ,

!1000 #This will run the command in 1000th line in history. !1000:p #Just previewing without running

Note: ‘fc’ is bash built-in command. So no external package is needed.

Caution: The above command will be executed once you run ‘fc’. So beware of ‘rm’ and similar commands. Try out in /tmp

Written by

No Comment

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *