TrumanWong

history

Display or operate the history list.

Summary

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

The main purpose

  • Show history list.

  • Operation history list.

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

  1. In the command line, you can use the symbol ! to execute the historical command with the specified serial number. For example, to execute the second historical command, enter !2.
  2. After closing the terminal, the history list will be written to the history file ~/.bash_history.
  3. The environment variable HISTSIZE determines the number of commands stored in the history file. The default value is 1,000.
  4. If the environment variable HISTTIMEFORMAT is a non-null value, use its value as the format string for strftime(3) to print the relevant timestamp and add it before each displayed history record; otherwise the timestamp will not be printed.
  5. This command is a built-in bash command. For related help information, please see the help command.