To use a command from history you can write like $!100. But sometimes I make a little change of the command.

Is there a way to get a command from history without executing it.

Now I'm copying and paste from history by using mouse. I want to know how to do it by using only keyboard.



shopt -s histverify

Using that, you'll get the expansion of the history command, and can edit it before executing. From the manual:

If the histverify shell option is enabled, and Readline is being used, history substitutions are not immediately passed to the shell parser. Instead, the expanded line is reloaded into the Readline editing buffer for further modification.

You can put this option into Bash's configuration files (e.g. ~/.bash_profile or ~/.bashrc depending on which you use) to have it enabled automatically.

  • So there is a shopt for it. I didn't know it. Thanks! – ironsand Jul 23 '13 at 19:52

In Bash, start typing the first few letters of the command and then hit Ctrl+R to start a history search. You can hit Ctrl+R to continue searching backwards in your Bash history. When you find the command you'd like, use the arrow keys to select/navigate around the command to edit and execute to your needs.

You can also hit Ctrl+R with an empty command buffer and start your search from there.

  • Thanks for your Information. But I prefer to use history command. – ironsand Jul 23 '13 at 20:02
  • 1
    You asked for (and received) an answer using only the keyboard. I'm not sure it gets any easier than using Ctrl+R. – Garrett Jul 23 '13 at 20:12
  • Sorry, I should stated more accurately what I want. I'll try to get used to the way that you showed me. – ironsand Jul 23 '13 at 20:18

In addition to the history search via ctrl+r already mentioned, you can use the history entry number method you are already familiar with:

!<history entry number>

in the middle of other commands like

echo !100

and even reference arguments from previous commands. For instance suppose in my history, entry 100 was:

ping google.com

I could do a host lookup on google by pulling the first argument from that history entry like so:

host !100:1

This works for the special reference to the previous command


as well. Of course for the numbered entries, you have to know their entry number by either typing history and looking, or piping to grep. If you want to pull from your history by matching the text, you can use the text from a previous command in the ! statement. For example using the fact that I have that ping google.com statement in my history, I can reference it like:

host !ping:1

The thing to keep in mind with the search by text method is that it searches backwards and pulls the first match.


You can replace any string in the previous command with the ^^ operator. Not sure what it is called. Otherwise (not last command), use Garrett's suggestion.

$ ping
PING ( 56(84) bytes of data.
64 bytes from icmp_req=1 ttl=64 time=0.333 ms
64 bytes from icmp_req=2 ttl=64 time=0.251 ms
--- ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.251/0.292/0.333/0.041 ms

magic here:

$ ^ping^finger
The program 'finger' is currently not installed.  You can install it by typing:
apt-get install finger
  • slhck mentions how to enable further modification of commands recalled with ^^. – Ярослав Рахматуллин Jul 23 '13 at 19:20
  • I never seen ^^ operator. Thanks for your tip! – ironsand Jul 23 '13 at 20:02

You can use:

cat ~/.bash_history

...to view the entries in your bash history. If you're looking for something specific, pipe it to grep and search. For example, if I ran a chmod command that I want to refer back to, I could run:

cat ~/.bash_history | grep chmod

...to find those entries. Hopefully I understand your question correctly.

  • Sorry, your information is not what I want to know. And you may use history command instead of cat ~/.bash_history. Thanks anyway! – ironsand Jul 23 '13 at 20:00
  • Yeah, no problem. Sorry it wasn't what you were looking for. – Jack Jul 24 '13 at 14:58

Is there a way to get a command from history without executing it?

Maybe I’m missing the point of the question, as I doubt that I’m the only person who knows about :p.  It stands for “print”.  If you type !100:p, the shell will print (i.e., display) command #100 without executing it.

If you don’t know about the : operators in history, you should learn.  As “numeric illustration” points out, :n retrieves the n th word of a command.  You can also use :n-m to get a range; and there are interesting defaults.  For example, :- (leaving n and m blank) gets you all the words except for the last one.  :$ gets you the last word.  :h is “head”, so if you type

ls -ld /asdfg/qwerty/foo/bar


!:- !$:h

turns into

ls –ld /asdfg/qwerty/foo

Oh, yeah, !$ is short for !:$:s is substitute, as in :s/old/new/.  This is the same as ^old^new^ except for things like

  1. :s can be used on any command (as in !100:s/...);  ^^ works only on the most recent command.
  2. :s can be used on individual words, or ranges of words.  For example,

    !:- !$:s/d/D/


    ls -ld /asDfg/qwerty/foo/bar

    (rather than ls -lD ...).

  3. :s can be applied globally.  The syntax is non-intuitive, but, where :s/old/new/ replaces the first occurrence, :gs/old/new/ replaces every occurrence.

Also, once you've fetched a command from history without executing it (e.g., with !100:p), possibly with modifications (as shown above), it becomes the most recent command in your history list.  Now you can press ↑ (cursor up) and edit it.

  • Thanks Scott. This is very useful information! May I ask you where can I found more Information? Is there a name of these options? – ironsand Aug 8 '13 at 4:03
  • @Tetsu: Try the bash man page, under the Modifiers heading. – Scott Aug 8 '13 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.