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.

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

4 Answers

up vote 3 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
add comment

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

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