8

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?

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

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

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

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
  • 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
8

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.

  • cool! this is also awesome – zzk Mar 9 '13 at 23:49
1

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

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

0

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

0

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] |

0

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.

  • 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 '15 at 5:57
0

Another option is to go to the beginning of the line and do the thing you were going to do followed by &&, like

rm old_file && cmd [args]

So if the thing you wanted to do succeeds, your command will run immediately afterwards, and if it fails you command won't run, but it'll be in history. You could also do

test && cmd [args]

Since test with no args will fail, but this is a handful of extra keystrokes compared to the # method.

0

I think the "some key strokes" you want are Ctrl-U and Ctr-Y which store a line in to the kill-ring, and then yank it back out later. It doesn't go into the history, but then I think it shouldn't, as you didn't do it.

Readline Killing Commands

Killing And Yanking

I learned this years ago, then forgot and went looking for those key strokes too.

It's discussed here too: https://unix.stackexchange.com/questions/140741/how-to-save-a-command-you-entered-without-executing-it

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.