46

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?

5 Answers 5

61

history -s command

3
  • 11
    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, 2011 at 15:17
  • 1
    @BenPage: Look at both answers here: unix.stackexchange.com/questions/112354/…. Jun 29, 2016 at 15:45
  • See help history for details.
    – l0b0
    Sep 11, 2020 at 8:23
13

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

7
  • C-a is a Readline Key Binding to move to beginning of the line (from man bash). I could not find C-j there but it seems it 's like ENTER. Nov 8, 2023 at 22:24
  • @Martian2020: C-j is bound to the Readline function accept-line as is C-m. You can find aacept-line in man bash. By the way, C-j is newline and C-m is carriage return. Nov 9, 2023 at 13:44
  • I could not find C-j in man bash on my Linux Mint based system (and C-m too), accept-line (Newline, Return) as opposed to beginning-of-line (C-a). Therefore I've written 'it seems'. One may try to look at bash source, but it is not high priority for me lol. Nov 10, 2023 at 0:41
  • @Martian2020: man ascii will show that C-j is newline and C-m is CR (return). These are basic well-known, well-defined characters that should never be remapped. Nov 10, 2023 at 13:59
  • 1
    @Martian2020: All control characters are offset from the corresponding letter by decimal 64 (hex 40). See this page: en.wikipedia.org/wiki/Control_character. Note that it uses another common representation: ^J. See also en.wikipedia.org/wiki/C0_and_C1_control_codes#ASCII where ^J, "Line Feed" and "end-of-line" are used. Nov 12, 2023 at 12:26
2

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
  • 4
    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, 2010 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. Apr 28, 2010 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. Apr 28, 2010 at 19:31
0

To build on @Spiff's answer, you can put the results of the command into the history, not just the command itself. For example, I often like a date time stamp in the history list:

> alias dts='history -s $(echo -n "### "; date)'
> dts
> history
...
45 ### Fri Sep 29 08:26:52 PM UTC 2023
0

Sometimes it can be useful to add a command even from a subprocess (which makes to lose the current command):

echo "the command" >> ~/.bash_history

You must log in to answer this question.

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