Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

6 Answers 6

up vote 1 down vote accepted

Run

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.

share|improve this answer
    
So there is a shopt for it. I didn't know it. Thanks! –  Tetsu 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.

share|improve this answer
    
Thanks for your Information. But I prefer to use history command. –  Tetsu Jul 23 '13 at 20:02
    
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. –  Tetsu Jul 23 '13 at 20:18

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.

share|improve this answer
    
Sorry, your information is not what I want to know. And you may use history command instead of cat ~/.bash_history. Thanks anyway! –  Tetsu 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

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.

share|improve this answer
    
Thanks! This is a very good lesson for me. :D –  Tetsu Jul 23 '13 at 19:49

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 10.3.0.3
PING 10.3.0.3 (10.3.0.3) 56(84) bytes of data.
64 bytes from 10.3.0.3: icmp_req=1 ttl=64 time=0.333 ms
64 bytes from 10.3.0.3: icmp_req=2 ttl=64 time=0.251 ms
^C
--- 10.3.0.3 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
finger 10.3.0.3
The program 'finger' is currently not installed.  You can install it by typing:
apt-get install finger
share|improve this answer
    
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! –  Tetsu Jul 23 '13 at 20:02

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 nth 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

then

!:- !$: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/

    gets

    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.
share|improve this answer
    
Thanks Scott. This is very useful information! May I ask you where can I found more Information? Is there a name of these options? –  Tetsu 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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