Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a range of commands from history

I am using Ubuntu and I'd like to repeat a bunch of commands I performed. I tried something like

for i in $(seq 2006 2013); do \!$i; done;

but failed, since shell tries to execute a command '!2006'.

man history

also didn't reveal to me how to repeat a range of commands.

like image 858
Andreas Gnyp Avatar asked Jul 11 '14 13:07

Andreas Gnyp


People also ask

How do you run a command in history?

Type “history” (without options) to see the the entire history list. You can also type ! n to execute command number n. Use !! to execute the last command you typed.

How do I run commands from history in Linux?

Another way to get to this search functionality is by typing Ctrl-R to invoke a recursive search of your command history. After typing this, the prompt changes to: (reverse-i-search)`': Now you can start typing a command, and matching commands will be displayed for you to execute by pressing Return or Enter.

Which command is used to see the history of the executed command?

history command is used to view the previously executed command.

How do you run a command from your bash history?

Bash includes search functionality for its history. The typical way of using this is through searching backwards in history (most recent results returned first) using the CTRL + R key combination. For instance, you can type CTRL + R , and begin typing part of the previous command.


2 Answers

If you are using bash (or ksh), the fc built-in allows you to manipulate history in various ways.

fc -l                   # list recent history.
fc -l      2006 2013    # list commands 2006..2013
fc         2006 2013    # launch editor with commands 2006..2013; executes what you save
fc -e pico 2006 2013    # launches the editor pico on command 2006..2013
fc -e -    2006 2013    # Suppresses the 'edit' phase (but only executes the first listed command)
fc -e :    2006 2013    # Launches the ':' command (a shell built-in) as the editor

The classic technique in ksh is to use an alias alias r='fc -e -', but because of the behaviour of bash, it is necessary to twist its arm a little harder and use alias r='fc -e :' instead.

like image 93
Jonathan Leffler Avatar answered Nov 13 '22 08:11

Jonathan Leffler


for i in $(seq 2006 2013); do \!$i; done; 

in your code, you may think ! is like ! in bash command line, but here "!" with the "$i" become a string like string command "!2006, !2007 ... !2013" but actually there is no command named "!2006" "!2006" in whole is a command name.

in Bash ! is a event designator. when you use !2006. it is explained as "reference to command 2006" but not use command "!2006".

! is always left to right execute command.

for more information please visit http://www.gnu.org/software/bash/manual/bashref.html#Event-Designators

I try the following way to get the same result:

for i in $(seq 2006 2013); do  fc -e - $i; done;
like image 37
Lookou Avatar answered Nov 13 '22 08:11

Lookou