Imagine you've just run a dozen commands. Say...

$ cd foo/    # history cmd #10000 (my history is very long)
$ ... more commands ...
$ cd ../     # history cmd #10012 

I know I can re-run them concatenated with !-12 && !-11 && !-10 && (and so on) && !! if they happen to have just been run (unlikely) or !10000 && !10001 && !10002 && (and so on), but is there a simpler way than hand-typing each history number with a bang and ampersands?

Is there perhaps some kind of range thing I'm unaware of in bash?

e.g. !{10000-10012} # something like this, only working.


This is what the fc command is for. For the last 12 commands:

fc -12 -1

or for commands numbered #10000 to #10012

fc 10000 10012

This is not quite what you want as it will launch an editor first, but that is probably a good thing since it gives you a chance to double check that you have the correct commands and even edit them using all the capabilities of your favorite editor. Once you save you changes and exit the editor, the commands will be run one after another.

  • Ah hah! Brilliant! fc. Fix Commands. Makes sense! – inanutshellus Oct 9 '12 at 16:42
  • 2
    To avoid launching an editor (dangerous!), you can use fc -e true -12 -1 – BingsF Jul 7 '16 at 21:40
  • 3
    @okwap, nonsense. It's POSIX specified and certainly available in Ubuntu. (It's a shell built-in, though—as it must be—so if you're looking in /bin you won't find it.) – Wildcard Nov 10 '16 at 12:23
  • 2
    Is the only way, if you decide once you're in the editor that you wish to abort, to delete the contents? (if I type :q!, Vim quits but the commands execute anyway). Here's the man page if it helps anyone else. – William Turrell Nov 16 '20 at 10:17
  • 2
    @WilliamTurrell You can try quitting the editor with a nonzero exit code, I'm told :cq will do this in Vim. – jw013 Nov 16 '20 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.