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. –  Oliver Charlesworth Mar 9 '13 at 0:46

7 Answers 7

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

Normally I just add a # to the begin of the line to transform the line into a comment:

#cmd [a long list of arguments] 

I prefer this way because you can do it with just 3 [4 if you need SHIFT to insert #] keystrokes

^a#<ENTER>

^a goes to the begin of current line

# adds the comment

ENTER executes the comment

share|improve this answer

You could try using this line:

history -s -p $COMMAND

From help:

The -p option means to perform
history expansion on each ARG and display the result,

This means you can expand things like !! and store the result at the bottom of history.

share|improve this answer
    
history -s was given in an answer two years ago.  Does the -p improve on that?  What does -p do?  (To the extent that it makes sense to do so, you should edit your answer to make it better, and not respond to my comment with another comment.) –  G-Man May 13 at 5:57

Your Answer

 
discard

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