history

Display or operate the history list.

Summary

history [-c] [-d offset] [n]
history -anrw [filename]
history -ps arg [arg...]

The main purpose

Options

-c clears the history list.
-d offset deletes records based on offset. If it is a positive number, it represents the record at the offset position. If it is a negative number, it represents the record at the offset position from the end forward.
-a Adds the current terminal's history lines to the history file.
-n Appends history lines that have not yet been read from the history file to the current history list.
-r Read the history file and append its contents to the history list.
-w Appends the current history list to the history file and appends them to the history list.
-p Perform history expansion on each arg and display the results on standard output without storing the results in a history list.
-s Append each arg as a single entry to the history list.

Parameters

n: Optional, only list the most recent n records.

filename: optional, represents the history file; the default calling order is filename, environment variable HISTFILE, ~/.bash_history.

return value

Returns successful unless an illegal option is provided or an error occurs.

example

Use the history command to display the 5 most recently used history commands.

[root@localhost ~]# history 5
    97 cd .ssh/
    98ls
    99 cat known_hosts
   100 exit
   101 history 10

Clear history

[root@localhost ~]# history -c

Delete specified row

[root@localhost ~]# history -d <specify line number>
[root@localhost ~]# history -d 2243

Quickly execute a historical command

# Execute the nth historical command
[root@localhost ~]# !n

# Execute the last command starting with xxx
[root@localhost ~]# !xxx

Notice