31

Some instances of bash change the command history when you re-use and edit a previous command, others apparently don't. I've been searching and searching but can't find anything that says how to prevent commands in the history from being modified when they're reused and edited.

There are questions like this one, but that seems to say how to cope with the history being edited. I've only recently come across an instance of bash that does edit the history when you reuse a command - all previous bash shells I've used have (as far as I've noticed) been configured not to change the history when you reuse and edit a command. (Perhaps I've just not been paying proper attention to my shell history for the past 15 years or so...)

So that's probably the best question: CAN I tell bash NEVER to modify the history - and if so, how?

IpsRich
  • 767
  • You can use history -p to do history expansions without affecting the history file. You can do history -s to do same and appending to history without execution. You can do HISTIGNORE='start of a command I never want in history.*:command2.*' to keep certain commands out of history. – mikeserv Sep 10 '14 at 10:47
  • 1
    Doesn't the set revert-all-at-newline on in the question you link solve your problem? Can you show how to reproduce the problem? Note that you can also use zsh for a behaviour more inline with what you want. – Stéphane Chazelas Sep 10 '14 at 11:03
  • @mikeserv Looking at the man page, history -p is something I would have to do for every command that I want to reuse - is that right? If so, that's not quite what I'm looking for. As I mentioned, I've used instances of bash that don't ever modify the history when a command is reused and edited. – IpsRich Sep 11 '14 at 12:21
  • @StéphaneChazelas No, I don't think revert-all-at-newline is quite what I need. If I edit a command and press CTRL-C to cancel it, I also don't want that to affect the history. I've never before had to configure bash not to modify the history, which is part of the reason I'm confused as to why sometimes a fresh install behaves in one way and sometimes in the other. By the way, on an instance that does not edit the history on command reuse, a bind -V | grep revert indicates that revert-all-at-newline is set to 'off'. – IpsRich Sep 11 '14 at 12:25
  • Since you can reproduce it, can you include the procedure you used (including the CTRL-C and the bash version) to reproduce it in your question. At the moment, I cannot reproduce it, though I know I've been annoyed by things like that with bash in the past. – Stéphane Chazelas Sep 11 '14 at 12:34
  • It might be that the fresh install isnt quite that fresh - the packagers can occasionally have something like a heavy hand in initial config. And also - and this is strictly opinion, mind you - bash kinda sucks. As shells go, you could do much better - bash's history handling is especially pretty much insane. Anyway, there is also the $HISTCONTROL variable which accepts various shopt values and the concept of a history comment character. You might also look at the myriad compvxx shopt settings to see if one matches the behavior you've grown used to. – mikeserv Sep 11 '14 at 13:11
  • Thanks for the comments. I can't for the life of me remember what instances of bash were annoying me, but as soon as I locate it I'll provide some more information. – IpsRich Sep 12 '14 at 14:22
  • "bash kinda sucks". Assuming that's true, what do you suggest replacing it with, @mikeserv? – Agi Hammerthief Dec 09 '14 at 14:15
  • @snotwaffle - well, zsh is the popular choice, but that way lies a tangled-knot of incompatibility (though it is still one I use on a daily basis). ksh93 is kind of amazing - though some people find the prompting difficult to get used to (not too hard to handle actually and another daily driver here). And dash is crazy fast, generally compliant in every way, and eminently configurable, but you will likely want to compile it yourself w/ the libedit build-time option (I'm 3-car-a-day kind of guy I guess). Mostly I regard bash's readline an interactive nightmare. – mikeserv Dec 09 '14 at 18:20
  • Thanks, @mikeserv. I think I'll give ksh a try. – Agi Hammerthief Dec 10 '14 at 11:09
  • 2
    Actually this has very little to do with bash. It's all about readline. There are different implementations of readline library, and it also has its own set of settings (A LOT of them) and its own configuration file. I'm not sure exactly if this is a setting or simply an implementation difference, but you should search in readline-related documentation, not bash. – orion Jan 23 '15 at 10:14
  • I think revert-all-at-newline may have something to do with this after all: there's another thread that claims this was the answer. However, when I turn revert-all-at-newline off it has no obvious effect: it does not suddenly enable me to edit my history! So there has to be something else somewhere that also needs to be changed. – IpsRich Nov 11 '15 at 13:15
  • @RichardWiseman The stock bash that comes with Ubuntu 16.04 exhibits this behaviour. Did you manage to fix it? – Pod Jul 27 '16 at 12:58
  • I've tried Ubuntu 16.04 and set revert-all-at-newline to on and yes, it worked! Now, there are no asterisks in my history and nothing gets changed therein. The only thing I have noticed is that you can't do set revert-all-at-newline on on the command line: it has to be in ~/.inputrc for it to take effect. – IpsRich Aug 01 '16 at 09:05

2 Answers2

19

Turns out revert-all-at-newline is the answer. I needed to include set revert-all-at-newline on in my ~/.inputrc file, since using the set command at the bash prompt had no effect. (Then, of course, I had to start a new shell.)

Also, I found that ~/.inputrc is loaded instead of /etc/inputrc if present, which means that any defaults defined in the latter are no longer active when you create ~/.inputrc. To fix this, start ~/.inputrc with $include /etc/inputrc.

Thanks to @StéphaneChazelas for pointing me in the right direction.

IpsRich
  • 767
0

In ~/.bashrc you can add

shopt -s histappend
Usi
  • 153
  • Thanks, but that has no effect on the problem I'm experiencing. I've just checked and histappend is on but my history still shows edits to previous commands. – IpsRich Feb 10 '16 at 13:20
  • I've searched the bash man page and I've found the rules for the editing history Readline Command Names

    It is fairly descriptive but I don't see an option for what you want except for maybe:

    redraw-current-line Refresh the current line.

    – Usi Feb 11 '16 at 02:22
  • There is also an revert-all-at-newline option that defaults to off – Usi Feb 11 '16 at 02:33