# Avoid duplicatesexportHISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting itshopt-shistappend
# After each command, append to the history file and reread itexportPROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
history [n]
history -c
history -d offset
history -anrw [filename]
history -p arg [arg ...]
history -s arg [arg ...]
With no options, display the command history list with line numbers. Lines listed with a *
have been modified. An argument of n lists only the last n lines. If the shell variable
HISTTIMEFORMAT is set and not null, it is used as a format string for strftime(3) to display
the time stamp associated with each displayed history entry. No intervening blank is printed
between the formatted time stamp and the history line. If filename is supplied, it is used
as the name of the history file; if not, the value of HISTFILE is used. Options, if sup‐
plied, have the following meanings:
-c Clear the history list by deleting all the entries.
-d offset
Delete the history entry at position offset.
-a Append the ``new'' history lines (history lines entered since the beginning of the
current bash session) to the history file.
-n Read the history lines not already read from the history file into the current history
list. These are lines appended to the history file since the beginning of the current
bash session.
-r Read the contents of the history file and use them as the current history.
-w Write the current history to the history file, overwriting the history file's con‐
tents.
-p Perform history substitution on the following args and display the result on the stan‐
dard output. Does not store the results in the history list. Each arg must be quoted
to disable normal history expansion.
-s Store the args in the history list as a single entry. The last command in the history
list is removed before the args are added.
If the HISTTIMEFORMAT variable is set, the time stamp information associated with each his‐
tory entry is written to the history file, marked with the history comment character. When
the history file is read, lines beginning with the history comment character followed immedi‐
ately by a digit are interpreted as timestamps for the previous history line. The return
value is 0 unless an invalid option is encountered, an error occurs while reading or writing
the history file, an invalid offset is supplied as an argument to -d, or the history expan‐ sion supplied as an argument to -p fails.
试想一下,我们操作 Linux 系统,如果把所有的命令都记录到 .bash_history 中,会不会有风险呢?
当然有风险啦,如果哪一天我们不幸中招,黑客攻入了我们的系统,他只要查看一下 history 就能知道我们的很多秘密,比如一些登录密码。为了避免该类事情的发生,我们希望 history 不要显示含有隐私信息的历史命令,只显示不含有隐私信息的命令。这个需求太个性化,但 history 仍然能够实现,下面我们就为大家介绍两种行之有效的解决方案。