How to prevent Bash from altering history?

26

9

If I press the Up or Down arrows on my keyboard and then modify something from my history, it's changed forever. Even if I press Ctrl-C and then try to bring it up again, it's still changed -- I've lost that entry in my history.

How can I prevent this from happening?

user541686

Posted 2011-06-25T21:52:37.943

Reputation: 21 330

Related to https://superuser.com/questions/1131241/bash-history-editing-unwanted

– Gray -- SO stop being evil – 2019-10-15T18:39:13.073

3I love the title of this question. You can actually alter history and you're complaining? What is wrong with you?! – Daniel Beck – 2011-06-25T22:24:02.647

@Daniel: Lol, yeah... I mean, it's as if I'm changing my past. Clearly nonsensical, and it gets ridiculously annoying after a while. :\ – user541686 – 2011-06-25T22:25:51.513

Btw, if you use history-search-xxx instead, the behavior's different. Maybe that's something for you?

– Daniel Beck – 2011-06-25T22:29:45.920

@Daniel: That's indeed useful -- thanks a lot. But still, I'd like to know the answer to this question, because sometimes the commands don't look so much like each other, and that doesn't work. – user541686 – 2011-06-25T22:34:20.183

history-search with an empty prompt works just like regular history prev/next, i.e. displaying all entries. – Daniel Beck – 2011-06-25T22:35:33.887

@Daniel: That's a way to bypass the problem, but it's not really a solution -- I've bound the two searches to different commands, and I don't want either of them to change my history. – user541686 – 2011-06-25T22:36:48.200

Answers

17

You want the readline setting:

set revert-all-at-newline on

You can either put it in ~/.inputrc (see note below), or put bind 'revert-all-at-newline on' in your ~/.bashrc.

Demo:

$ man bash
$ bind 'set revert-all-at-newline on'
$ man bsh # up arrow and edit
No manual entry for bsh
$ man bash # three up arrows

Further details are in the Bash manpage:

revert-all-at-newline

If set to ‘on’, Readline will undo all changes to history lines before returning when accept-line is executed. By default, history lines may be modified and retain individual undo lists across calls to readline. The default is ‘off’.


Note:

If a new ~/.inputrc file is created for the purpose of setting revert-all-at-newline, be aware that bash will use the readline settings in this file instead of any settings which may be in the file /etc/inputrc. That is, any settings specified in /etc/inputrc will no longer be in effect. Therefore, if the /etc/inputrc file exists, it's a good idea to start ~/.inputrc with the line:

$include /etc/inputrc

sarnold

Posted 2011-06-25T21:52:37.943

Reputation: 2 988

You're awesome, that's exactly what I need. No longer do I want to punch my monitor. :D +1000000 (I wish) – user541686 – 2011-06-26T00:08:16.037

5

I enter:

ls /tmp

- wonderful. Now I wan't to enter

ls /temp 

and can prevent it to enter the history, therefore prevent it to overwrite ls /tmp, if I start the command with a blank:

 ls /temp

It's hard to see, but if you know it ...

It is controlled by

export HISTCONTROL=ignoredups
export HISTCONTROL=ignoreboth

ignoredups only ignores duplicated commands, ignoreboth ignores spaces at the beginning of line, which is useful, to hide otherwise unhidden passwords.

But maybe you're out for a solution, where you end with both commands, the unmodified old one, and the new one. My version of bash or settings behave like this, but I don't know, what's different to yours.

user unknown

Posted 2011-06-25T21:52:37.943

Reputation: 1 623

Ha; I always assumed not saving commands with a leading space was a bug. Thanks. :) – sarnold – 2011-06-25T23:58:12.060