Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
done

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

Thanks!

share|improve this question

2 Answers 2

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

 
discard

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.