12

By pressing up, I can go through previously entered commands. I've noticed though that if I modify one of them, that alters the history. For instance, if I type:

echo a
echo b
echo c
[up][up][backspace]d[ctrl+c]

the history now shows that the second command was echo d, not echo b. How do I keep the first echo b in the history?

For instance, say I run a really long command, with a lot of options. then I want to run it again, with a minor change, so I go back through the history to find it, make the change, but then realize that actually I don't need to re-run it, having just thought of another option to do instead. then later, I want to go back and remind myself what the command I ran was - but wait, the history is now showing the wrong thing!

This comes up very rarely, but when it does I find it really irritating. Is there some way to automatically preserve the original history?

2
  • 1
    with history you can see all the command. After you can select what you need. Which shell are you using now?
    – Hastur
    May 23, 2014 at 11:37
  • 6
    I can't reproduce that. If I change c to d, press CTRL+C and press [up] again, it still says echo c. May 23, 2014 at 11:38

2 Answers 2

8

Try to put in your ~/.inputrc

 set revert-all-at-newline on

In some case you can find it in the default value (Off).
It should force readline to undo all changes to history lines before returning when accept-line is executed. (more info in man bash).

Edit:
CTRL+c and set revert-all-at-newline on works fine since bash-4.3.30.

# GNU bash, version 4.3.30(1)-release
# Emacs-mode

echo c
[up][backspace]d[Ctrl+c][up] # you should see echo c

But:

echo c
[up][backspace]d[down][Ctrl+r]echo[Ctrl+j][Ctrl+c][up] # you should see echo d
2
  • Nice, this is the better solution. Interestingly, CTRL-c will actually cause it to remember the modified value, though fully erasing the line or arrowing down and then pressing enter or running another command will restore the history. May 24, 2014 at 13:25
  • @DeanSerenevy: Thanks I've like your workaround too and I start to use CTRL-a in nano too :-)
    – Hastur
    May 27, 2014 at 6:03
4

Usually:

CTRL-a # ENTER

will do it.

The original command will be remembered if you execute the modified command. Of course, you don't want to actually execute, so you can comment out the line with #.

In your example, you would then have in your history: echo a, echo b, echo c, # echo d.

1
  • 1
    You can press alt-shift-3 to comment out the current command line if your bash is using readline. /insert-comment in man 3 readline (or man bash) for more in info.
    – cychoi
    Jul 24, 2015 at 3:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .