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.
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- UNIX Bash - Removing double quotes from specific s
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
- Spread 'sed' command over multiple lines
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 theHISTFILE
, 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:Here is your bash history toolkit...
Exit bash without writing anything
This is hacky and aggressive. But it's a fun way to end an espionage session.
Clear history from memory
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
This rereads the
$HISTFILE
and appends it to the history in memory (if there is any). You can do this afterhistory -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
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 ofhistory 1
(which is identical tohistory | 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:
This also works:
Even this works:
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...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.
Or to also get rid of the original mistake...
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 becausehistory 1
returns not just the number, but also the command and its arguments, and each would get a separate cycle in thefor
loop. But at this point this is turning into abash
lesson and I'll stop.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).That should do:
unset the HISTFILE.
The following works for me.
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.
Unset the
$HISTFILE
variable