This page looks best with JavaScript enabled

Using the history Command

 ·  ☕ 2 min read

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

1
set +o history

Turn on command history

1
set -o history

2.2 Method Two: Use the HISTCONTROL Environment Variable

1
export HISTCONTROL=ignorespace

This way, commands prefixed with a space are not recorded. For example:

1
 echo "this command will not be recorded"

2.3 Method Three: Disable history by Setting HISTSIZE

1
export HISTSIZE=0

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.

1
history -d <number>

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:

1
2
3
set +o history
history -d <number>
set -o history

3.2 Clearing the Current Session History

1
history -c

Completely clear the history of the current session

1
2
3
set +o history
history -c
set -o history

3.3 Completely Deleting the History

history -c can only delete the execution history of the current session

1
2
3
set +o history
rm ~/.bash_history || rm ~/.zsh_history
set -o history

WeChat Official Account
WRITTEN BY
WeChat Official Account