Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a quick interactive search at a Bash prompt, press Ctrl-R, type part of the command, then press Ctrl-R again to move to older matches. Press Enter to run the displayed command, or edit it first. To list matches instead, use history | grep -i -- 'term'. To search commands saved from earlier sessions, search Bash’s history file with grep -in -- 'term' "${HISTFILE:-$HOME/.bash_history}".

Choose the search method that fits

Need Command or shortcut What it searches or does
Find and reuse one command quickly Ctrl-R Searches backward through the current shell’s history interactively.
List all matching entries in the current shell history | grep -i -- 'term' Filters numbered entries in the in-memory history list, without running them.
List matching entries newest first fc -ln -r | grep -i -- 'term' Lists history text without line numbers and reverses its order before filtering.
Search saved history on disk grep -in -- 'term' "${HISTFILE:-$HOME/.bash_history}" Searches the configured history file, or ~/.bash_history when HISTFILE is unset.

In the grep examples, replace term with a distinctive fragment such as kubectl or tar. The -i option ignores letter case; -- marks the end of options so a search fragment beginning with a hyphen is treated as a pattern.

Search interactively with Ctrl-R

  1. At a Bash prompt, press Ctrl-R.
  2. Type a distinctive part of the command you want to find.
  3. Press Ctrl-R again to move to older matches.
  4. When the desired command appears, press Enter to execute it, or use editing keys to change it before execution.

Readline names this shortcut reverse-search-history: it searches backward from the current line through the history list. This is convenient when you want to recall one command, but accepting a match executes it; inspect and edit it first if you are unsure what it will do.

List matching commands without executing them

Search the current shell’s history

Run:

history | grep -i -- 'term'

history prints numbered entries, and grep displays the lines that contain the fragment. This is useful when you want to compare several matches before deciding which one to reuse.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Show command text newest first

Run:

fc -ln -r | grep -i -- 'term'

Here, -l lists entries, -n omits their numbers, and -r reverses the selected history range. The fc builtin can list, edit, or re-execute history entries; use its listing options when you only want to inspect results.

Search commands saved from earlier sessions

To search the history file Bash is configured to use, run:

grep -in -- 'term' "${HISTFILE:-$HOME/.bash_history}"

Bash’s HISTFILE variable identifies the file used to save history; its documented default is ~/.bash_history. This command searches the file on disk, not necessarily commands from the current shell: Bash commonly writes session history when the shell exits, so commands not yet written may not appear.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inspect a specific history entry safely

The history command displays numbered entries. To show the last 20, use:

history 20

To list a specific range, use the entry numbers shown by history:

fc -l 123 130

Prefer listing and reviewing an entry before reusing it. fc -s and accepting a command from interactive search can re-execute a command, which may have effects beyond simply displaying it.

Why a command may be missing

Bash maintains an in-memory history list when history is enabled. The GNU Bash manual specifies a default HISTSIZE of 500 commands; the setting controls how many commands are retained in that list. The saved-file size is controlled separately by HISTFILESIZE.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the relevant settings with:

  • echo "$HISTCONTROL" and echo "$HISTIGNORE" — ignore and duplicate-handling rules can exclude commands.
  • echo "$HISTSIZE" and echo "$HISTFILESIZE" — low limits can remove older entries from memory or the saved file.
  • echo "$HISTFILE" — an unset variable uses Bash’s default history path, while a custom or unwritable path can affect saving.

Bash generally saves history at configured write points, commonly when a shell exits. The histappend setting determines whether the history file is appended to or overwritten when the shell saves history. Multi-line command behavior is also affected by cmdhist and lithist.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Display dates alongside history entries

Set a timestamp format before running history:

HISTTIMEFORMAT='%F %T '

Then run history to display timestamps with entries. When timestamp formatting is enabled, Bash records timestamp markers in the history file and uses them to delimit entries when reading it.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.