Don't save current bash session to history

2020-05-12 10:55发布

I notice that when opening .bash_history that it contains only the entries from my previous session, it seems that the current session is appended only on exit. Is there any way to prevent the current session from saving? Even crashing bash is an option if one knows how to do that. I found that I can kill -9 the process, but if there is a better way I would love to know.

标签: bash
7条回答
别忘想泡老子
2楼-- · 2020-05-12 11:32

There's another option, similar to history -c, but that does not wipe anything previous to the current session.

It is history -r, which reloads the history from the HISTFILE, like if you just logged in.

I don't know if this works or is available in any bash version previous to 4.3.11, but I though it would be useful to include it to the list.

Here's an example showing the difference between this command and the -c one:

user@host:~$ # I Just logged in
user@host:~$ history | tail -n6 # this shows commands from the previous session, which ends at item 4682 as well as from the current one
 4679  2014-08-23 17:15:29 # Previous session
 4680  2014-08-23 17:15:33 # Still the previous session
 4681  2014-08-23 17:15:37 # note the datetime
 4682  2014-08-23 17:15:44 exit
 4683  2014-08-23 17:17:25 # I Just logged in
 4684  2014-08-23 17:19:54 history | tail -n6 # this shows the last command, and the ones from the previous session
user@host:~$ # This is a secret command, so I need to remove the traces of this session
user@host:~$ history -r
user@host:~$ history | tail -n5 # Note that I went back to item 4682, and there are no traces of history -r command
 6242  2014-08-23 17:15:29 # Previous session
 6243  2014-08-23 17:15:33 # Still the previous session
 6244  2014-08-23 17:15:37 # note the datetime
 6245  2014-08-23 17:15:44 exit
 6246  2014-08-23 17:22:26 history | tail -n5 # Note that I went back to item 4682, and there are no traces of history -r command
user@host:~$ history -c # instead if I issue history -c
user@host:~$ history # everything disappears
 5248  2014-08-23 17:23:13 history # everything disappears
user@host:~$
查看更多
爷的心禁止访问
3楼-- · 2020-05-12 11:32

Here is your bash history toolkit...

Exit bash without writing anything

kill -9 $$

This is hacky and aggressive. But it's a fun way to end an espionage session.

Clear history from memory

history -c

This clears memory of all history. If you hit the up arrow, you get nothing. Your $HISTFILE is untouched. You can prove this with...

Reload history from disk

history -r

This rereads the $HISTFILE and appends it to the history in memory (if there is any). You can do this after history -c to regain the ability to Ctrl+R search or up arrow for previous commands. (You can do this instead of logging out and back in).

Note: If you didn't clear history first, this just appends to the current history in memory. This will obscure the history so that hitting the up arrow a few times will give you comfort in thinking that what you wanted to hide is gone. In reality it is just buried and will be written to disk unless it is deeper than your $HISTSIZE or $HISTFILESIZE.

Execute a command without including it in history

echo foo bar baz; history -d $(history 1)

This uses history -d to delete an entry by number. Since only the first argument is used (and others are ignored) we can use the output of history 1 (which is identical to history | tail -n 1) to get the number of the current entry.

Because bash oneliners are single history entries, you can do multiple commands like so:

echo foo; echo bar; echo baz; history -d $(history 1)

This also works:

echo foo \
bar \
baz; history -d $(history 1)

Even this works:

for x in foo bar baz; do
echo $x
done; history -d $(history 1)

Delete your password (a command, etc.) from your history

If all you are concerned about is getting rid of a single entry, you can use the previous example creatively. Use history to find the number of the entry to delete. Then delete it by number. For example...

$ history
  1  pwd
  2  date
  3  sudovisudo
  4  hunter2
  5  man history
  6  help history
  7  history
$ history -d 4

I hope I don't have to tell you this, but just in case: Don't grep history for your password. If you "really do" need to search history, do history | LESSHISTFILE=/dev/null less, and explicitly do a / search.

If you are really embarrassed and want there to be no record of you deleting something from history, you can combined this concept with the last.

history -d 4; history -d $(history 1)

Or to also get rid of the original mistake...

for n in "$(history 1)" 4 3; do history -d $n; done

Notice that you have to cycle over the entry numbers in decending order because each call to history -d pops the entry out of the list and all subsequent entries' numbers decrease by 1. Also, you have to double quote the subshell because history 1 returns not just the number, but also the command and its arguments, and each would get a separate cycle in the for loop. But at this point this is turning into a bash lesson and I'll stop.

查看更多
Bombasti
4楼-- · 2020-05-12 11:47

Perhaps more elegant than crashing bash would be to use the history -c command to clear the history of the current session. Then, there's nothing to save (it even wipes itself from the history).

查看更多
手持菜刀,她持情操
5楼-- · 2020-05-12 11:51

That should do:

HISTFILE=

unset the HISTFILE.

查看更多
▲ chillily
6楼-- · 2020-05-12 11:54

The following works for me.

 export HISTFILE=/dev/null 

Note that it has a space in front of it. Most modern distros would not add commands that are entered after a space to bash history. That will prevent that line also from appearing in your history.

查看更多
The star\"
7楼-- · 2020-05-12 11:55

Unset the $HISTFILE variable

$ unset HISTFILE

If HISTFILE is unset, or if the history file is unwritable, the history is not saved.
http://www.faqs.org/docs/bashman/bashref_106.html

查看更多
登录 后发表回答