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.

Every now and then it would be useful to add a command directly to the history in Bash without actually executing it. So far the closed thing I have found is adding a # in front of it and hitting return. Are there better ways?

share|improve this question

3 Answers 3

up vote 13 down vote accepted

history -s command

share|improve this answer
    
Never knew about that feature. I can't think of an immediate use for it, but it's good to know. –  Doug Harris Apr 28 '10 at 21:18
1  
Do you know if it is possible to run a similar command to this from a script? I have a script ./resize-images.sh whose final line indicates a command you should run to undo the changes, it would be really useful if instead the script could add the command to my history so I just hit up-enter if i needed to undo –  Ben Page Jul 7 '11 at 15:17

history -s command

You can even bind a keystroke to do this for you. You can enter this at a Bash prompt:

bind '"\C-q": "\C-a history -s \C-j"'

or add this to your ~/.inputrc:

"\C-q": "\C-a history -s \C-j"

then you can type something and press Ctrl-q and it will be added to the history without being executed. The space before "history" causes the history command itself to not be added to the history if your HISTCONTROL variable contains ignorespace or ignoreboth. Another keystroke could be chosen instead of "\C-q".

share|improve this answer

It is hard to come up with anything shorter than a one character "command", so we really need to go to the keystroke level. With my bash setup and an US keyboard what you do now takes:

  1. Go to beginning of line: CTRLA: +1 instruction, +2 keypresses
  2. Add # (needs SHIFT): +1 instruction, +2 keypress
  3. Appending it to the history with ENTER: +1 instruction, +1 keypress

so in total 3 instructions, 5 keypresses.

Using ALT# does the same in 1 instruction, 2 keypresses.

YMMV depending on your keyboard layout and bash configuration.

share|improve this answer
2  
The problem isn't the number of key presses, but that "#command" gets stored in the history, instead of the intended "command". –  Grumbel Apr 28 '10 at 19:08
    
It's nice that "#command" gets stored in the history file as it lets you know that the command wasn't actually executed. This might be nice if you later wanted to review what commands you entered in order to figure out a problem. –  Marnix A. van Ammers Apr 28 '10 at 19:22
    
@Grumbel, @Marnix: Agreed, I didn't answer the question to the letter but showed how to make what he does now even faster. Personally I think using #command is way quicker than history -s command, and also very easy to use if you map history-search-backward/-forward properly. But it's a matter of taste. –  Benjamin Bannier Apr 28 '10 at 19:31

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.