194

For example:

[root@ip-10-0-7-125 ~]# history | grep free
  594  free -m
  634  free -m | xargs | awk '{print "free/total memory" $17 " / " $ 8}'
  635  free -m
  636  free -m | xargs | awk '{print "free/total memory" $9 " / " $ 10}'
  736  df -h | xargs |  awk '{print "free/total disk: " $11 " / " $9}'
  740  df -h | xargs |  awk '{print "free/total disk: " $11 " / " $8}'
  741  free -m | xargs | awk '{print "free/total memory: " $17 " / " $8 " MB"}'

I'm just wondering if there any way to execute the 636 command without typing it again, just type something plus the number, like history 636 or something.

2
  • 22
    do you know about ctrl-r? Apr 8, 2016 at 8:27
  • 3
    Yep, i know that but it's not exactly what i want.
    – The One
    Apr 8, 2016 at 9:04

5 Answers 5

334

In bash, just !636 will be ok.

4
  • 16
    In Linux - This has more to do with the shell (bash, zsh, etc) a person is running then the OS you are running it on. There are shells on linux without support for history.
    – Zoredache
    Apr 8, 2016 at 18:06
  • @Zoredache And similarly, this works fine in most implementations of bash on Windows.
    – Ajedi32
    Apr 8, 2016 at 18:14
  • 1
    Not just bash, of course, but other shells too - including csh.
    – Drew
    Apr 9, 2016 at 15:19
  • 13
    If you've got a command that's very similar to what you want to run, you can append :p to print the command without running it, !636:p for example. Which means that you can easily select it again from your history by pressing the up arrow and make the minor changes you want
    – AJefferiss
    Apr 14, 2016 at 7:27
97

Yes, it's called "history expansion." See

LESS='+/^HISTORY EXPANSION' man bash

for full details.

Using an exclamation point followed by a number is arguably the simplest use:

!636

However, you can also run the last executed command directly without knowing its history number:

!!

Or you can run two commands back:

!-2

The form I most often use, however, is to repeat the last argument of the last command:

echo this text goes into a file > /tmp/afile.txt
cat !$

Or, let's say I'm inspecting a file. Using tab completion, I might do the following:

ls -l /really/long/path/to/some/file
less !$

Again, read the man page for more details:

LESS='+/^HISTORY EXPANSION' man bash
8
  • 28
    Interactively, Alt-. can be more convenient than !$. It inserts the last argument of the last command verbatim into the line.
    – ulidtko
    Apr 8, 2016 at 12:08
  • 2
    @ulidtko Where are those special escapes documented (I knew about !$, but never heard about Alt-.)? Will you post a link to it, please?
    – edmz
    Apr 8, 2016 at 13:26
  • 5
    LESS="+/DEFAULT KEY BINDINGS" man 3 readline, /etc/inputrc, ~/.inputrc. Many programs besides bash use libreadline; for those who do, you get the same features like Alt-., Ctrl-X-U (undo), Ctrl-/ (describe shortcut), tab-completion, etc -- for free. For example, with just three lines of PYTHONSTARTUP one can enable tab-completion and history in the stock python REPL. Which will work (mostly) the same as in bash, since it's the GNU Readline library that does the work. The readline's manpage describes the features in detail.
    – ulidtko
    Apr 8, 2016 at 14:44
  • 2
    Also, Ctl-UpArrow (or AltUpArrow on bash) will scroll backwards through the history list. Typing a few characters then the UpArrow gets only the commands starting with that letter, e.g. "fr(Ctl-Up)" scrolls through all the "free..." commands in the OP's list. This also works in things like gnuplot & gdb that use readline.
    – jamesqf
    Apr 8, 2016 at 17:06
  • 2
    @Ajedi32, see the final bolded line in my answer. :) Truthfully this answer is barely a taste of what history expansion can do. Keep in mind that this is at the level of the shell, not the command you are calling; hence echo {1..5} followed by touch !$ will touch five files, not just one. So strictly speaking it's not the last argument but the last shell word on that line. (Also try echo hello >temp followed by echo goodbye !$.) If you can think of a way to put it more clearly feel free to suggest an edit.
    – Wildcard
    Apr 8, 2016 at 18:36
19

A nice one, if you don't want to first history, note the number, etc:

in bash (and maybe others):

ctrl-r something 

(ctrl-r = "reverse search interactively") (something = a part of a previous command)

This will show you the latest history line containing something . To get the one before, do ctrl-r (alone) again, each time it gets a previous line.

ex:

ctrl-r  10

to show the latest history line containing '10' (for exemple the line with $10, in your exemple), and ctrl-r again until you retrieve the history line you were looking for

When the line you wanted appear, just Enter to run it (or you can also edit it, using the arrow keys, backspace, and ctrl-a to go to the beginning of the line, ctrl-e to get to the End, ctrl-k : to "kill" from cursor to end of line (=deletes+saves it in a buffer), ctrl-y : to restore, from the buffer, a previously killed bit, etc.)

If you want to search forward (ctrl-s), you first need to disable XON : see https://stackoverflow.com/a/791800/1841533 :

" just to disable XON/XOFF by running

stty -ixon

" (and then you will be able to use ctrl-s, without having it freeze the terminal)

8
  • If I do this and accidentally press ctrl-r one time too many, is there some way to search forward in the history again?
    – kasperd
    Apr 9, 2016 at 20:33
  • @kasperd: Yes you can. many answers on se points it out : ctrl-s for forward search. Apr 9, 2016 at 20:36
  • Unfortunately ctrl-s is used to freeze the terminal, and bash doesn't disable that, so that doesn't work in the default configuration. (Other programs - such as emacs - disables the keybinding to freeze the terminal such that ctrl-s can be used for searching.)
    – kasperd
    Apr 9, 2016 at 20:43
  • @kasperd : Yes, sorry for not mentionning that earlier (I pointed "many answers" because they do precise that, but I didn't took the time to type this in the comment or give a link... (I was using my phone). I now came back home and could edit that in the answer above. ctrl-s for bash history search needs you to stty -ixon first Apr 10, 2016 at 0:17
  • That command is going to break normal use of ctrl-s, so that's not really an option.
    – kasperd
    Apr 10, 2016 at 1:45
10

You can use the shell builtin fc:

fc -s 636
5
  • 3
    This is actually better than the accepted answer, but you have to remove the -s to get the benefits. If you run fc 636 you will open an editor with command 636 from your history in the buffer, and when you save and exit the command (modified or not) will be run. And even better, this one is POSIX-compliant.
    – Wildcard
    Oct 24, 2017 at 1:56
  • @StevenPenny Thanks. And yes, I do know what POSIX is, though I don't see the relevance. Regardless, some easily accessed and included documentation would be nice. Nov 11, 2017 at 17:17
  • @FaheemMitha try running "help fc."
    – Wildcard
    Nov 11, 2017 at 18:40
  • @Wildcard help is not POSIX - might have to do man dash or similar
    – Zombo
    Nov 11, 2017 at 18:59
  • @Wildcard Thanks. I tend to forget help exists. Another shell builtin. And puzzling for unwary users who might want help with a new system, but instead are confronted with a stream of gibberish. :-( Nov 11, 2017 at 19:55
7

There is HSTR - hstr command for bash and zsh interactive command selection, better than Ctrl-R reverse search:

hstr interactive history command example

It's interactive, so you can search for, and edit the command, before executing it.

On Ubuntu, you can install hstr with the following one-liner:

sudo add-apt-repository ppa:ultradvorka/ppa && sudo apt-get update && sudo apt-get install hstr && hstr --show-configuration >> ~/.bashrc && . ~/.bashrc

or step by step:

sudo add-apt-repository ppa:ultradvorka/ppa
sudo apt-get update
sudo apt-get install hstr

Install hstr on Fedora, RHEL or CentOS:

sudo dnf install hstr -y

On macOS with homebrew:

brew install hstr

...then configure it with:

hstr --show-configuration >> ~/.bashrc

This will replace the default Ctrl-R behavior.

Run hstr --show-configuration to determine what will be appended to your Bash profile.

More configuration options available on project homepage at Github: https://github.com/dvorka/hstr

1
  • 1
    HSTR is great! Note: The package to install is hstr not hh The config to profile command sets an 'hh' alias.
    – Matthew
    Mar 26, 2021 at 22:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .