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?



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.


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.

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.