Repeat last command N times

5,854

Solution 1

With zsh, and provided the last command line was only one command or pipeline or and-or list (that is for instance echo x, echo x | tr x y, echo x && echo y, even compound commands like { x; y; } or for/while loops but not echo x; echo y):

repeat 30 !!

To repeat the previous command line even if it contained several commands, use:

repeat 30 do !!; done

Or:

repeat 30 {!!}

With bash and for simple-commands only (among the examples above, only the echo x case), you could define a helper function like:

repeat() {
  local n="$1"
  shift
  while ((n-- > 0)); do
    "$@"
  done
}

(and use repeat 30 !! like above). A side effect is that because the code will be running in a function, it will see different "$@", "$#" and things like typeset will work differently, so you can't do things like:

eval 'echo "$1"'
repeat 30 !!

Another approach to emulate zsh's repeat 30 {!!} would be to declare an alias like:

alias repeat='for i in $(seq'

(assuming an unmodified $IFS)

And then use:

repeat 30); { !!; }

Solution 2

The shortest I can come up with is:

date # or whatever command
for i in {1..30}; do !!; done

Solution 3

One approach could be to use the line editor to insert !!; 30 times.

Like with readline (bash's line editor) in vi mode:

!!;Escdd30p

The emacs mode equivalent does work with the zsh line editor but apparently not with bash's readline. However you could use readline kbd macros instead which apparently can be repeated:

Define the kbd macro as !!;:

Ctrl+X(!!;Ctrl+X)

Which you can later invoke 30 times as:

Alt+3Alt+0Ctrl+Xe

Solution 4

This is a bit ugly, but:

 eval "`fc -ln -1`;: "{1..10}\;

The leading space is not strictly necessary, but is useful to suppress entering the eval command into history if $HISTCONTROL contains ignorespace (or ignoreboth).

Alternatively:

eval "fc -s $((HISTCMD-2)) "{1..10}\;

And:

eval 'history -s '{1..10}';fc -s -2;'
Share:
5,854

Related videos on Youtube

f1rstsurf
Author by

f1rstsurf

Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C. In other words: the choice of C is the only sane choice. I know Miles Bader jokingly said "to piss you off", but it's actually true. I've come to the conclusion that any programmer that would prefer the project to be in C++ over C is likely a programmer that I really would prefer to piss off, so that he doesn't come and screw up any project I'm involved with. The only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C. And limiting your project to C means that people don't screw that up, and also means that you get a lot of programmers that do actually understand low-level issues and don't screw things up with any idiotic "object model" crap. So I'm sorry, but for something like git, where efficiency was a primary objective, the "advantages" of C++ is just a huge mistake. The fact that we also piss off people who cannot see that is just a big additional advantage. -- Linus Torvalds

Updated on September 18, 2022

Comments

  • f1rstsurf
    f1rstsurf over 1 year

    Short of writing a loop, is there a way to repeat the last command N times.

    For example, I can repeat the last command once by using a double bang (!!), but how do I repeat it say 30 times?

  • f1rstsurf
    f1rstsurf almost 7 years
    That has a loop.
  • Stéphane Chazelas
    Stéphane Chazelas almost 7 years
    You can shorten it to for i in {1..30};{ !!;}