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.

Is there a way to save a command in bash history without executing it? e.g.

$ cmd [a long list of arguments] 

and now during typing i remember I'd like to do something else first. Can I have something like

$ cmd [a long list of arguments][some-key-strokes]

and this does not actually execute but goes into bash history so that I can use it later?

share|improve this question

migrated from stackoverflow.com Mar 11 '13 at 3:49

This question came from our site for professional and enthusiast programmers.

In that situation, I just prefix the line with #, and then hit Enter. –  Oliver Charlesworth Mar 9 '13 at 0:46

5 Answers 5

up vote 4 down vote accepted

There may be a better way, but you can use history expansion's :p modifier to print the current comment line without executing. !# is the current line, and % by itself will match nothing

$ cmd [arguments] !#%:p
share|improve this answer
cool! ;) and without need of escaping special chars in the command what can get messy if the command is not just ls -al +1 –  hek2mgl Mar 9 '13 at 0:50
cool! very neat –  zzk Mar 9 '13 at 3:36

Use the -s option to the history command:

history -s cmd arg1 arg2

The call to history -s itself, conveniently, is not added to the command history, so in your history it appears as if you executed cmd without actually doing so.

share|improve this answer
cool! this is also awesome –  zzk Mar 9 '13 at 23:49

Usually this is done by

echo 'cmd [a long list of arguments]' >> /home/you/bash_history

Note that the name of the history can differ on your system. Therefore you can use the HISTFILE environment var

what makes:

echo 'cmd [a long list of arguments]' >> "$HISTFILE"
share|improve this answer

In that situation, I just prefix the line with #, and then hit Enter.

... or use the key combo: ESC #

bind -p | grep -i 'insert-comment'   # "\e#": insert-comment

See also: Bash - save command without executing it

share|improve this answer

You can try adding a space and | (pipe) at the end of the line, hitting enter, and then Ctrl-C'ing.

(Space, |, Enter, Ctrl+C)

The command will be in your history with a | at the end (can simply remove with backspace) instead of a # at the beginning (potentially more tedious to remove).

DISCLAIMER: I don't know if this always works! Use at your own risk!

$ cmd [long arguments list] |

> Ctrl+C

$ Up

$ cmd [long arguments list] |

share|improve this answer

Your Answer


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