history

shell builtinLinux/Unix
The history command is one of the most frequently used commands in Linux/Unix-like operating systems. history Display or manipulate the command history list

Quick Reference

Command Name:

history

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

history [options] [arguments]

Common Use Cases

    Syntax

    history [options] [n]

    Options

    Option Description
    -c Clear the history list
    -d offset Delete the history entry at position offset
    -a Append history lines to the history file
    -n Read all history lines not already read from the history file
    -r Read the history file and append the contents to the history list
    -w Write the current history to the history file
    -p Perform history expansion on the args and display the result without storing in the history list
    -s Add the args to the history list as a single entry

    Examples

    How to Use These Examples

    The examples below show common ways to use the history command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    history
    Display the entire command history list.
    history 10
    Display the last 10 commands.
    history -c
    Clear the command history list. # Advanced Examples Advanced history -d 5 Delete the 5th entry in the history list. history -w Write the current history to the history file. history | grep ssh Search command history for commands containing "ssh". !42 Execute command number 42 from the history list. !! Execute the last command again.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The history command displays, manages, and manipulates the command history in shell environments like Bash. It allows users to recall previously executed commands, making it easier to repeat complex commands or track what actions have been performed in a session. Key features of history: 1. Command Recall: history maintains a list of recently executed commands, allowing users to review what they've done and easily repeat commands without retyping them. 2. Numbered Entries: Each command in the history is assigned a sequential number, which can be used with the '!' history expansion character to recall and execute specific commands. 3. History File: Commands are typically stored in a history file (e.g., ~/.bash_history in Bash), which persists between sessions, allowing users to recall commands from previous sessions. 4. History Management: Options like -c (clear), -d (delete), -w (write), and -r (read) provide control over the history list and its interaction with the history file. 5. History Expansion: Special characters like '!' enable history expansion, allowing quick reuse of previous commands (e.g., '!!' repeats the last command, '!ssh' repeats the most recent command starting with 'ssh'). 6. Customization: The behavior of history can be customized through environment variables like HISTSIZE (number of commands to remember), HISTFILESIZE (number of lines in history file), and HISTCONTROL (controls which commands are saved). 7. Integration with Command-Line Editing: In most shells, history is integrated with command-line editing features, allowing users to navigate through history using arrow keys or search it with keyboard shortcuts like Ctrl+R. The history command is particularly useful for system administrators and power users who frequently execute complex commands or need to track their actions. It enhances productivity by reducing repetitive typing and helps with documentation and troubleshooting by providing a record of executed commands.

    Related Commands

    These commands are frequently used alongside history or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the history command works in different scenarios.

    $ history
    View All Commands