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?

  • 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. – Richard Wiseman 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'. – Richard Wiseman Sep 11 '14 at 12:25
  • 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

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.

| improve this answer | |

In ~/.bashrc you can add

shopt -s histappend
| improve this answer | |
  • 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. – Richard Wiseman 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. – Jeff Coleman Feb 11 '16 at 2:22
  • There is also an revert-all-at-newline option that defaults to off – Jeff Coleman Feb 11 '16 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.