Save command to history without executing in bash

Solution 1:

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

Solution 2:

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.

Solution 3:

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

Solution 4:

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"