I'm working in Mac OSX, so I guess I'm using bash...?
Sometimes I enter something that I don't want to be remembered in the history. How do I remove it?
I'm working in Mac OSX, so I guess I'm using bash...?
Sometimes I enter something that I don't want to be remembered in the history. How do I remove it?
If you want to run a command without saving it in history, prepend it with an extra space
prompt$ echo saved
prompt$ echo not saved \
> # ^ extra space
For this to work you need either ignorespace
or ignoreboth
in HISTCONTROL
. For example, run
HISTCONTROL=ignorespace
To make this setting persistent, put it in your .bashrc
.
If you've already run the command, and want to remove it from history, first use
history
to display the list of commands in your history. Find the number next to the one you want to delete (e.g. 1234) and run
history -d 1234
Additionally, if the line you want to delete has already been written to your $HISTFILE (which typically happens when you end a session by default), you will need to write back to $HISTFILE, or the line will reappear when you open a new session:
history -w
bash
settings are stored in RAM while the shell is running. The rc
files are only for storing persistent settings for the next time the shell starts.
– jw013
Sep 26 '12 at 18:03
history -a
, in that case it is already written to the history file, rather than on exit under normal configuration. Specifically: http://mywiki.wooledge.org/BashFAQ/088
– jordanm
Oct 22 '12 at 03:54
history
to find the command in which I typed a secret command, I can use history -d
to delete it but someone could simply scroll up my screen and see it.
– s g
Dec 07 '17 at 01:00
HISTCONTROL=ignorespace
, then invoke two history -d
commands with a leading space - one for deleting your original command and one for deleting the ignorespace assignment.
– maxschlepzig
Oct 12 '18 at 19:57
while true; do str=\
history | grep -m 1 YOUR_GREP_HERE | awk '{print $1}'`; if [[ ! $str ]]; then break; fi; history -d $str; done`
– CletusW
Dec 03 '18 at 23:45
history -w
does not exist oh RHEL 7 the reason why the commands still appear for zsh users is that you also have to delete the entry from .zsh_history AND very important to logout and login again.
– Matthis Kohli
May 07 '19 at 12:00
ctrl-p
it shows up even it is not saved in the history @jw013
– alper
Jul 15 '20 at 23:40
man history
show anything about -d
, yet it worked? GNU History 8.1, 2020 July 17.
– Gauthier
Sep 15 '21 at 15:49
history
is also a bash builtin. type history
reveals that. Use man bash-builtins
to find the man page for it.
– Dario Seidl
Sep 28 '21 at 14:57
196994 2024-01-25-23:17I inadvertently typed my password instead of "su"
196995 2024-01-25-23:17history
alex@Freesia:~$ history -d 196994
`bash: history: 196994: history position out of range``
– Alexandre Oberlin
Jan 25 '24 at 22:18
history -d <line number>
behaved strangely for me because of my PROMPT_COMMAND:
export PROMPT_COMMAND="history -a;history -r"
Setting PROMPT_COMMAND=
in a terminal before calling history -d <line number>
did the trick.
Beware however that you cannot delete several entries in one command and that line numbers change after you have deleted an entry.
– Alexandre Oberlin
Jan 26 '24 at 18:02
To clear all your history, use
history -c
To delete a single line, use
history -d linenumber
history
– David Sawyer
Aug 10 '21 at 22:21
history
. I'm not familiar with it, as you can maybe tell.
– Clonkex
Aug 11 '21 at 00:17
I have this in my ~/.bashrc
, which makes the command $ forget
delete the previous command from history
function forget() {
history -d $(expr $(history | tail -n 1 | grep -oP '^\s*\d+') - 1);
}
history -d $( history | tail -n 1 | cut -f 1 -d " " )
be simpler?
– seumasmac
Oct 03 '15 at 01:40
history | tail -n1
is the history
command itself, so deleting that number gets the wrong entry. However, history -d $( history | awk 'END{print $1-1}' )
combines the line select, field select, and subtraction.
– dave_thompson_085
Oct 03 '15 at 03:03
You always can edit and remove entries from ~/.bash_history
, useful when you want to remove either one entry or more than one entry
If you want to forget the entire bash session, you can kill
the current bash process. Since the variable $$
hold the pid
of the current shell, you can do:
kill -9 $$
$HISTFILE
into account.
– Kusalananda
Apr 02 '20 at 08:02
$HISTFILE
immediately, this should work. The ONLY think I have taken into account is the $HISTFILE
. What experience have you had (with my proposal) that causes you concern?
– Bruno Bronosky
Apr 13 '20 at 15:39
$HISTFILE
in the current session either, so I really don't see how that comment could be misinterpreted. The only thing you have to add to your answer is the assumptions under which the given solution solves the problem. There is really only one assumption, namely that the command that should be removed from the history hasn't yet been written to $HISTFILE
. Then you may want to elaborate under what conditions the command would have been written to $HISTFILE
.
– Kusalananda
Apr 13 '20 at 15:53
To remove a single line from the history file, use the -d option. For example, if you want to clear a command where you entered the clear-text password as in the scenario above, find the line number in the history file and run this command.
$ history -d 2038
To delete or clear all the entries from bash history, use the history command below with the -c option.
$ history -c
Alternatively, you can use the command below to delete the history of all last executed commands permanently in the file.
$ cat /dev/null > ~/.bash_history
Also With Bash 5, you can delete a range aswell
history -d 511-520
1- in bash terminal type
history
# This will list all commands in history .bash_history file with line numbers
ex:
...
987 cd
988 ssh x@127.0.0.1
990 exit
991 cd
2- pick the CMD line number you want to delete
history -d 988
Note: if you want to delete for example last 3 CMDs, just pick the third line number from bottom ex: 988 and repeat the CMD history -d 988
3 times in sequence.
history
command lists the used commands I just wanted to explain that!
– KhogaEslam
Mar 16 '20 at 12:26
You need to write the changes after you cleared the history. And if you wouldn't like to have the history wipe command in your history then you need to run the command like that:
history -c && history -w && logout
Good luck.
In Ubuntu (but I'm pretty sure, it will be work for other Linux distributions and also MacOS the same way) the bash history file can be simply edited in an arbitrary text editor:
$ nano ~/.bash_history
If you don't know, where it's stored, you can find it as follows:
$ echo $HISTFILE
Or you can just do it a bit more generic way:
$ nano $HISTFILE
If you have hstr
(a way better reverse-i-search
that can be installed with sudo apt install hstr
), then it's really simple:
Ctrl+r
to search your history.delete
key, and press y
to confirm.If you want to delete a range of history lines, you can use the script below.
This example will delete history output from line 1 to line 150.
for i in `history | awk 'NR > 1 && NR <=150{print $1}'`; do history -d $i; done
Quick steps:
echo $HISTFILE
history
command in your terminal
history -d <line_number>
didn't work for me, so just deleted the desired line and the one preceding it (that should start with#
) from~/.bash_history
. – toraritte Jun 30 '21 at 20:10macOS Catalina
on, the standard shell iszsh
and notbash
and thehistory
command works differently, sohistory -d <line>
will not work. @toraritte answer becomes then: typevim .zsh_history
and delete typingd
once on the line you need to delete. To save and quit then type:x
and hitEnter
. – giotto Aug 10 '21 at 12:30