1. The history Command
The history command lets you view the command history of the current session. When you exit the shell, the command history is saved to the corresponding history file.
For bash, the command history is saved in the ~/.bash_history file; for zsh, it is saved in ~/.zsh_history. You can also use the HISTFILE environment variable to specify the location of the history file: export HISTFILE=/data/ops/bash_history.
2. How to Hide Commands from Being Recorded
2.1 Method One: Temporarily Turn Off history
- Current session
Turn off command history
| |
Turn on command history
| |
2.2 Method Two: Use the HISTCONTROL Environment Variable
| |
This way, commands prefixed with a space are not recorded. For example:
| |
2.3 Method Three: Disable history by Setting HISTSIZE
| |
3. How to Delete History
3.1 Deleting a Single History Entry
The history command shows the numbers of historical commands, and the -d option deletes them.
| |
Although it can delete a record at a given line number, this delete command itself will be recorded.
If you need to hide the history completely, you can use the following approach:
| |
3.2 Clearing the Current Session History
| |
Completely clear the history of the current session
| |
3.3 Completely Deleting the History
history -c can only delete the execution history of the current session
| |
