Linux command
history 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Display command history
history
Show the last N commands
history [20]
Clear the entire history
history -c
Delete a specific history entry
history -d [42]
Append current session history
history -a
Read history file
history -r
Write current history
history -w
Execute a command
![42]
Execute the previous command
!!
说明
history is a bash builtin command that displays or manipulates the command history list. The shell maintains a record of commands entered during a session, allowing quick access to previous commands. Without arguments, history displays the numbered list of commands. With an integer argument n, it shows the last n entries. The history is loaded from HISTFILE at shell startup and can be written back with -w or automatically on exit. History expansion allows re-executing previous commands: !! repeats the last command, !n executes command number n, !string executes the most recent command starting with string, and !?string matches any command containing string. Use Ctrl+r for interactive reverse search.
参数
- -c
- Clear the history list
- -d _OFFSET_
- Delete the history entry at position offset
- -d _START-END_
- Delete the range of history entries between start and end
- -a
- Append new history lines to the history file
- -n
- Read history lines not already read from the file to current list
- -r
- Read the history file and append contents to the history list
- -w
- Write current history to the history file
- -p
- Perform history substitution on args and display without storing
- -s _ARG_
- Add args to the end of the history list as a single entry
FAQ
What is the history command used for?
history is a bash builtin command that displays or manipulates the command history list. The shell maintains a record of commands entered during a session, allowing quick access to previous commands. Without arguments, history displays the numbered list of commands. With an integer argument n, it shows the last n entries. The history is loaded from HISTFILE at shell startup and can be written back with -w or automatically on exit. History expansion allows re-executing previous commands: !! repeats the last command, !n executes command number n, !string executes the most recent command starting with string, and !?string matches any command containing string. Use Ctrl+r for interactive reverse search.
How do I run a basic history example?
Run `history` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -c do in history?
Clear the history list