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?


history -s command

  • 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
  • 7
    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
  • 1
    @BenPage: Look at both answers here: unix.stackexchange.com/questions/112354/…. – RustyTheBoyRobot Jun 29 '16 at 15:45
  • See help history for details. – l0b0 Sep 11 '20 at 8:23

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".


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.

  • 3
    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
  • 1
    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
  • 2
    @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

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.