Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I want to execute a sequence of four commands that I have executed before. If the first one is 432 in the command-history, then I could do:

$ !432; !433; !434; !435

I'm curious, is there a more efficient way to accomplish this?

share|improve this question
Not posting this as an answer because it doesn't execute the commands, but you could type history -p \!43{2..5} to print commands 432 through 435 to the terminal, suitable for subsequent copy/paste. – Wildcard Mar 30 at 0:28
up vote 31 down vote accepted

If it refers to commands run just recently, a more efficient way is to reference them with negative numbers:

!-4; !-3; !-2; !-1

Also, once you do it, your last history entry will contain the whole chain of commands, so you can repeat it with !!.

Edit: If you haven't already, get familiar with the great builtin function fc, mentioned by Gilles. (Use help fc.) It turns out that you can also use negative numbers with it, so you could do the same as above using

eval "`fc -ln -4 -1`"

This has one caveat, though: after this, the eval line is stored in the history as the last command. So if you run this again, you'll fall into a loop!

A safer way of doing this is to use the default fc operation mode: forwarding the selected range of commands to an editor and running them once you exit from it. Try:

 fc -4 -1

You can even reverse the order of the range of commands: fc -1 -4

share|improve this answer
I think the word you were looking for is 'reinvoke'. It's great info though. +1 – BentFX Nov 16 '11 at 19:28
I just read what I'd written... LOL. Actually, I'm not sure about reinvoke (re-invoke maybe?), but thanks for telling :) I always appreciate corrections. – rozcietrzewiacz Nov 16 '11 at 19:38
Scrabblefinder.com says reinvoke is a valid Scrabble word. Can't get any more definitive than that. :) – BentFX Nov 16 '11 at 19:47
With a good answer like this one (+1), it doesn't matter how you spell it; especially as neither spelling form shows up in 3 major English dictionaries: Oxford, Camgridge, Merriam-Webster (I'd go for the hyphen) – Peter.O Nov 17 '11 at 4:55

To view a range of commands in the history use the built-in fc command:

fc -ln 432 435

To execute them again:

eval "$(fc -ln 432 435)"
share|improve this answer
Very nice. I'm curious though, why man fc doesn't give me anything. What does fc stand for? What other options are there? – Eric Wilson Nov 17 '11 at 3:00
@Eric Wilson ... type help fc... and also help help .... help: help [-dms] [pattern ...] Display information about builtin commands. – Peter.O Nov 17 '11 at 4:57
I just came back here, searching for your answer. This really is a great builtin! Especially when you is its default functionality - invoking an editor to modify a range of commands to be replayed. Thanks, Gilles. – rozcietrzewiacz Jul 24 '12 at 7:21
Without -l flag, it will execute without need for the evil eval. – Bernhard Dec 9 '13 at 14:59
@Bernhard There's nothing evil about eval. This isn't javascript. Understand the tool and don't arbitrarily toss shade because it's de rigueur. – user973810 Jul 30 '15 at 18:49

There is a nice and alternate way to run a number of commands in sequence from the Bash history:
instead of using history substitute (!432 or !-4), you can search through the history with Ctrl+r, and once you've found the first command you want to run, hit Ctrl+o (operate-and-get-next) instead of the return key
This will launch the command and propose the next one from the history. You can hit Ctrl+o as many time as you wish, and end the sequence either with return for a last one, or Ctrl+c to stop without launching it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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