Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

How can I execute a series of commands from my bash history? For instance if I want to execute the commands numbered 34 to 42 in my bash history, I've tried things like:

for n in $(seq 34 42); do
   exec \!$n;

but that doesn't really it do the the trick, does it. Any ideas?


share|improve this question
up vote 6 down vote accepted

The best I can think of is something like this. Basically you pipe history into awk which prints lines with a number in a certain range. The results are passed to a sub-shell.

history | awk '$1>32 && $1<42 {$1="";print substr($0,2)}' | bash

Somewhat based on this question.

share|improve this answer

Unfortunately, it seems that History (and the equivalent fc command) is disabled in scripting. I haven't found any documentation as to why though, the closest being the tldp page explaining History and Bash.

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.