Execute a range of commands from history

history, linux, shell, ubuntu

Solution

If you are using `bash` (or `ksh`), the `fc` built-in allows you to manipulate history in various ways.

fc -l                   # list recent history.
fc -l      2006 2013    # list commands 2006..2013
fc         2006 2013    # launch editor with commands 2006..2013; executes what you save
fc -e pico 2006 2013    # launches the editor pico on command 2006..2013
fc -e -    2006 2013    # Suppresses the 'edit' phase (but only executes the first listed command)
fc -e :    2006 2013    # Launches the ':' command (a shell built-in) as the editor

The classic technique in `ksh` is to use an alias `alias r='fc -e -'`, but because of the behaviour of `bash`, it is necessary to twist its arm a little harder and use `alias r='fc -e :'` instead.

Problem

I am using Ubuntu and I'd like to repeat a bunch of commands I performed. I tried something like ``` for i in $(seq 2006 2013); do \!$i; done; ``` but failed, since shell tries to execute a command '!2006'. ``` man history ``` also didn't reveal to me how to repeat a range of commands.

Original source