Repeat last command N times

Clash Royale CLAN TAG#URR8PPP
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?
bash command-line command-history
add a comment |
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?
bash command-line command-history
add a comment |
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?
bash command-line command-history
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?
bash command-line command-history
bash command-line command-history
edited Jun 21 '17 at 13:43
Jeff Schaller
40.1k1054126
40.1k1054126
asked Jun 21 '17 at 12:33
Tyler DurdenTyler Durden
1,62042250
1,62042250
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
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); !!;
add a comment |
The shortest I can come up with is:
date # or whatever command
for i in 1..30; do !!; done
2
That has a loop.
– Tyler Durden
Jun 21 '17 at 12:54
4
You can shorten it tofor i in 1..30; !!;
– Stéphane Chazelas
Jun 21 '17 at 13:05
add a comment |
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
add a comment |
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;'
@StéphaneChazelas good point, thanks.
– ecatmur
Jun 21 '17 at 14:38
add a comment |
The seq command is part of standard *nix and therefore not dependent on your shell. Using it and your shell's loop construct you can do things like this:
for i in $(seq 30); do !!; done
or
for i in `seq 30`; do !!; done
add a comment |
Another loop in bash (e.g. after this: https://unix.stackexchange.com/a/372487/9689 )
Make it a executable file somewhere in your $PATH named repeat to achieve similar effect like in zsh (https://unix.stackexchange.com/a/372484/9689):
#!/bin/bash
n=$1
shift 1
for ((x=0; x<$n; ++x)); do
"$@"
done
so later you can:
$ repeat 3 date
gist:
https://gist.github.com/gwpl/26fefe3a165304f3dedb21009506107f
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f372483%2frepeat-last-command-n-times%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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); !!;
add a comment |
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); !!;
add a comment |
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); !!;
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); !!;
edited Jun 21 '17 at 13:02
answered Jun 21 '17 at 12:45
Stéphane ChazelasStéphane Chazelas
303k56570925
303k56570925
add a comment |
add a comment |
The shortest I can come up with is:
date # or whatever command
for i in 1..30; do !!; done
2
That has a loop.
– Tyler Durden
Jun 21 '17 at 12:54
4
You can shorten it tofor i in 1..30; !!;
– Stéphane Chazelas
Jun 21 '17 at 13:05
add a comment |
The shortest I can come up with is:
date # or whatever command
for i in 1..30; do !!; done
2
That has a loop.
– Tyler Durden
Jun 21 '17 at 12:54
4
You can shorten it tofor i in 1..30; !!;
– Stéphane Chazelas
Jun 21 '17 at 13:05
add a comment |
The shortest I can come up with is:
date # or whatever command
for i in 1..30; do !!; done
The shortest I can come up with is:
date # or whatever command
for i in 1..30; do !!; done
answered Jun 21 '17 at 12:53
Jeff SchallerJeff Schaller
40.1k1054126
40.1k1054126
2
That has a loop.
– Tyler Durden
Jun 21 '17 at 12:54
4
You can shorten it tofor i in 1..30; !!;
– Stéphane Chazelas
Jun 21 '17 at 13:05
add a comment |
2
That has a loop.
– Tyler Durden
Jun 21 '17 at 12:54
4
You can shorten it tofor i in 1..30; !!;
– Stéphane Chazelas
Jun 21 '17 at 13:05
2
2
That has a loop.
– Tyler Durden
Jun 21 '17 at 12:54
That has a loop.
– Tyler Durden
Jun 21 '17 at 12:54
4
4
You can shorten it to
for i in 1..30; !!;– Stéphane Chazelas
Jun 21 '17 at 13:05
You can shorten it to
for i in 1..30; !!;– Stéphane Chazelas
Jun 21 '17 at 13:05
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Jun 21 '17 at 13:25
answered Jun 21 '17 at 13:11
Stéphane ChazelasStéphane Chazelas
303k56570925
303k56570925
add a comment |
add a comment |
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;'
@StéphaneChazelas good point, thanks.
– ecatmur
Jun 21 '17 at 14:38
add a comment |
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;'
@StéphaneChazelas good point, thanks.
– ecatmur
Jun 21 '17 at 14:38
add a comment |
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;'
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;'
edited Jun 21 '17 at 14:37
answered Jun 21 '17 at 14:24
ecatmurecatmur
16518
16518
@StéphaneChazelas good point, thanks.
– ecatmur
Jun 21 '17 at 14:38
add a comment |
@StéphaneChazelas good point, thanks.
– ecatmur
Jun 21 '17 at 14:38
@StéphaneChazelas good point, thanks.
– ecatmur
Jun 21 '17 at 14:38
@StéphaneChazelas good point, thanks.
– ecatmur
Jun 21 '17 at 14:38
add a comment |
The seq command is part of standard *nix and therefore not dependent on your shell. Using it and your shell's loop construct you can do things like this:
for i in $(seq 30); do !!; done
or
for i in `seq 30`; do !!; done
add a comment |
The seq command is part of standard *nix and therefore not dependent on your shell. Using it and your shell's loop construct you can do things like this:
for i in $(seq 30); do !!; done
or
for i in `seq 30`; do !!; done
add a comment |
The seq command is part of standard *nix and therefore not dependent on your shell. Using it and your shell's loop construct you can do things like this:
for i in $(seq 30); do !!; done
or
for i in `seq 30`; do !!; done
The seq command is part of standard *nix and therefore not dependent on your shell. Using it and your shell's loop construct you can do things like this:
for i in $(seq 30); do !!; done
or
for i in `seq 30`; do !!; done
answered Jun 22 '17 at 1:28
AlwaysLearningAlwaysLearning
26113
26113
add a comment |
add a comment |
Another loop in bash (e.g. after this: https://unix.stackexchange.com/a/372487/9689 )
Make it a executable file somewhere in your $PATH named repeat to achieve similar effect like in zsh (https://unix.stackexchange.com/a/372484/9689):
#!/bin/bash
n=$1
shift 1
for ((x=0; x<$n; ++x)); do
"$@"
done
so later you can:
$ repeat 3 date
gist:
https://gist.github.com/gwpl/26fefe3a165304f3dedb21009506107f
add a comment |
Another loop in bash (e.g. after this: https://unix.stackexchange.com/a/372487/9689 )
Make it a executable file somewhere in your $PATH named repeat to achieve similar effect like in zsh (https://unix.stackexchange.com/a/372484/9689):
#!/bin/bash
n=$1
shift 1
for ((x=0; x<$n; ++x)); do
"$@"
done
so later you can:
$ repeat 3 date
gist:
https://gist.github.com/gwpl/26fefe3a165304f3dedb21009506107f
add a comment |
Another loop in bash (e.g. after this: https://unix.stackexchange.com/a/372487/9689 )
Make it a executable file somewhere in your $PATH named repeat to achieve similar effect like in zsh (https://unix.stackexchange.com/a/372484/9689):
#!/bin/bash
n=$1
shift 1
for ((x=0; x<$n; ++x)); do
"$@"
done
so later you can:
$ repeat 3 date
gist:
https://gist.github.com/gwpl/26fefe3a165304f3dedb21009506107f
Another loop in bash (e.g. after this: https://unix.stackexchange.com/a/372487/9689 )
Make it a executable file somewhere in your $PATH named repeat to achieve similar effect like in zsh (https://unix.stackexchange.com/a/372484/9689):
#!/bin/bash
n=$1
shift 1
for ((x=0; x<$n; ++x)); do
"$@"
done
so later you can:
$ repeat 3 date
gist:
https://gist.github.com/gwpl/26fefe3a165304f3dedb21009506107f
answered Jan 12 at 20:40
Grzegorz WierzowieckiGrzegorz Wierzowiecki
5,2421363105
5,2421363105
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f372483%2frepeat-last-command-n-times%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown