Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash history uso: View and manage command history
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash history uso: View and manage command history

Overview

The history command in Bash is used to display the list of previously executed commands in the current shell session. It allows users to recall and reuse commands without having to retype them, enhancing productivity and efficiency in the command line.

Usage

The basic syntax of the history command is as follows:

history [options] [arguments]

Common Options

  • -c: Clears the entire history list.
  • -d offset: Deletes the history entry at the specified offset.
  • -a: Appends the new history lines to the history file.
  • -r: Reads the history from the history file and adds it to the current session.
  • -n: Reads the history lines not already read from the history file.

Common Examples

  1. Display the entire command history:
    history
    
  2. Display the last 10 commands:
    history 10
    
  3. Clear the command history:
    history -c
    
  4. Delete a specific entry from history (e.g., entry 5):
    history -d 5
    
  5. Append new commands to the history file:
    history -a
    
  6. Read the history from the history file:
    history -r
    

Tips

  • Use the ! operator followed by a command number to quickly execute a previous command (e.g., !100 executes the command listed as number 100 in your history).
  • Combine history with grep to search for specific commands in your history, like this:
    history | grep "git"
    
  • Regularly clear your history if you are concerned about privacy, especially on shared systems.