Linux bash history Command: Search, Re-run and HISTSIZE Settings

bash history command linux - custom-history-featured.png

bash history on Linux is one of the most used features — and a common leak vector when passwords are typed on the command line. Learn to search efficiently and set sane HIST* variables in ~/.bashrc.

Related: bash aliases,
environment variables,
shell scripting guide.

List and re-run

history numbers commands; !N re-executes entry N.

bashexamples
history | tail
!!   # repeat last command
!ssh  # last command starting with ssh
history
!N

Interactive search

Ctrl+R searches backward through history — fastest way to find a long docker or kubectl line.

Ctrl+R
search

Size and file

Control memory and disk use with HISTSIZE and HISTFILESIZE.

bashbashrc
export HISTSIZE=5000
export HISTFILESIZE=10000
export HISTCONTROL=ignoredups:erasedups
HISTSIZE
~/.bash_history

Security habit

Avoid putting secrets on the command line; use env files or prompts.

WarningSpace-prefixed commands (with HISTCONTROL=ignorespace) skip logging — do not rely on that for secrets.
HISTCONTROL
secrets

Quick Reference

  • history and !! / !prefix
  • Search: Ctrl+R
  • Tune HISTSIZE in ~/.bashrc

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.