tags:

views:

94

answers:

7

Sometimes I have to run a command many times in succession, for example to see if a service has started, and it becomes tedious to move my hands away from my normal typing position to press the Up Arrow and Enter keys repeatedly. Is there a way to run the previous command without the Up Arrow and Enter keys, perhaps with an elaborate shell script?

I've tried the following, but it is unsatisfactory because it cannot execute aliases, and it is a little slow.

history | tail -2 | head -1 | cut -d' ' -f4- | cat > prev_command.txt
sleep .01
chmod 777 prev_command.txt
eval prev_command.txt
rm prev_command.txt

Ideally I'd have an alias to this script so I can type in something like "prev" in the command line and hit Enter to run the previous command again.

+2  A: 

In bash, you can press ctrlp to go to the previous command -- that's a lot better than having to move to the arrow keys.

Mark Rushakoff
This also works in `tcsh`.
ire_and_curses
In ksh and zsh using "r" also works.
Lee-Man
A: 

Depending on what terminal you're using, I know a lot used to have F3 as an option for repeating, but that's still outside the normal range for typing as well unless you have a special keyboard with more accessible function keys.

My keyboard makes the function keys easily accessible, but I don't do much command line work in unix any more, so I wouldn't be able to tell you for sure whether or not this is still possible.

md5sum
+3  A: 

Use

!!

to run your previous command.

sudo !!

also works , for the record.

Greg
Is there a way to put !! or the ctrl p in the answer below into an alias? I've tried alias l='!!', but it gives me a "bash: !!: command not found" error when I run it.
+3  A: 

Instead of running the same command many times in succession, why not watch it instead? watch will run a specified command repeatedly and display the output in stdout so you can see it change over time.

watchcommand

dogbane
+1  A: 

Are you an emacs or vi user? You can use

set -o vi 
set -o emacs

to set emacs or vi keybindings. You can then use the emacs or vi key bindings in bash. I don't know if this should work for other shells. I believe the vi mode starts in insert mode, so you need to hit esc to enter command mode. In emacs mode (the default), you can use ctrl+p and then ctrl+j to move to the previous line and do a carriage return.

Otherwise, you can use !! as someone else suggested.

Kizaru
+1  A: 

I often use the "history expansion" feature in bash (usually activated with cntlR) -- it interactively searches through your history for the previous closest match.

See the bash manual section Searching for Commands in the History, and also Using History Interactively.

Ether
+2  A: 

In bash:

$ help fc
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
    Display or execute commands from the history list.

    fc is used to list or edit and re-execute commands from the history list.
    FIRST and LAST can be numbers specifying the range, or FIRST can be a
    string, which means the most recent command beginning with that
    string.

    Options:
      -e ENAME  select which editor to use.  Default is FCEDIT, then EDITOR,
            then vi
      -l    list lines instead of editing
      -n    omit line numbers when listing
      -r    reverse the order of the lines (newest listed first)

    With the `fc -s [pat=rep ...] [command]' format, COMMAND is
    re-executed after the substitution OLD=NEW is performed.

    A useful alias to use with this is r='fc -s', so that typing `r cc'
    runs the last command beginning with `cc' and typing `r' re-executes
    the last command.

    Exit Status:
    Returns success or status of executed command; non-zero if an error occurs.

Note the suggestion for alias r; I use this frequently.

Roger Pate