Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable editing my history in bash

Tags:

In bash, when I go back in history, edit some command and run it, this edited command is appended to history and the original one is left intact. But every once in a while I somehow manage to affect the original command, i.e. my edit replaces the original command back in history. I can't put my finger on how this happens.

Can someone explain? My goal is to avoid this, so any edit to a previous command always gets appended to history and never replaces the original.

like image 806
ultracrepidarian Avatar asked Mar 16 '10 23:03

ultracrepidarian


People also ask

How do I turn off bash history?

How to permanently disable bash history using set command. Again add set +o history to the end of to a new /etc/profile. d/disable. history.

How do I change bash history in Linux?

If you want to delete a particular command, enter history -d <line number> . To clear the entire contents of the history file, execute history -c . The history file is stored in a file that you can modify, as well. Bash shell users find it in their home directory as .

What does set +o history do?

set +o history - Does not write any of the current session to the log. Can be ran at any time during the session and will hide all commands. set -o history - Turns logging back on but logs the set command so obvious something has happened.

Can you clear bash history?

The user can remove all bash history or a specific history by using 'history' command. But there are many other commands to remove history information permanently. You can also remove history by removing the content of the . bash_history file.


2 Answers

I somehow manage to affect the original command, i.e. my edit replaces the original command back in history.

Right. If you go back in your history and edit the line without pressing return to execute the command but instead moving to another history entry, you've just edited the history entry. If you then list your history, you will see a * on the line indicating that you edited it. I find this "feature" immensely frustrating. Others have provided good examples of how to reproduce this.

My goal is to avoid this, so any edit to a previous command always gets appended to history and never replaces the original.

I too wanted to disable it. I found the solution via this answer over on unix.stackexchange.

To summarize, you need to enable the revert-all-at-newline readline setting which is off by default. If the setting is on then bash will revert any changes you made to your history when you execute the next command.

To enable this setting in your shell, you should add the following to your ~/.inputrc file and then restart your shell:

$include /etc/inputrc
set revert-all-at-newline on

The first line is needed because I guess that if you supply your own .inputrc file the default /etc/inputrc file is not included which is probably not what you want.

like image 118
Gray Avatar answered Dec 19 '22 23:12

Gray


If you go back to some previous command and edit it, but then DON'T execute it (instead using history commands to go to some other command and execute it), then the edits will remain there in your history list.

Pressing Ctrl + C, after editing, counters this behaviour. It leaves the original command in tact i.e. it cancels remembering edits to the original.

like image 38
Chris Dodd Avatar answered Dec 20 '22 01:12

Chris Dodd