Skip to main content

history command

Detailed Explanation

The history command is a shell built-in that displays a list of previously executed commands in the current session and previous sessions (read from the history file, typically ~/.bash_history for bash). It is invaluable for recalling long commands or seeing what actions were taken previously.

Basic Syntax

history [n]
history -c
history -d offset

Common Flags/Options

OptionDescription
nDisplay the last n commands.
-cClear the history list.
-d <offset>Delete the history entry at position <offset>.
-wWrite the current history to the history file.

Real-world Examples

Show the entire command history:

history

Show the last 10 commands:

history 10

Execute a command from history by its number (e.g., command #150):

!150

Search history for a specific command (e.g., finding the last grep command):

history | grep grep

(Alternatively, use Ctrl+R for an interactive reverse search).

Clear the current session's history:

history -c