Better Bash history


By default, history is stored in the file ~/.bash_history, we can change ~/.bashrc

Sync Bash History Between Terminals

  • shopt -s histappend: when close a session, the history will be appended to the .bash_history file rather than overwriting.

Increase Bash History Size

  • HISTSIZE: the number of lines or commands that are stored in memory in a history list (the default value is 500).
  • HISTFILESIZE: the maximum number of lines contained in the history file (the default value is 500).

Store bash history immediately

  • PROMPT_COMMAND="$PROMPT_COMMAND; history -a"
    • Store but don’t reload, so Up/Down is per shell.
  • PROMPT_COMMAND="PROMPT_COMMAND; history -a; history -c; history -r;"
    • Store and reload the history immediately
  • PROMPT_COMMAND: if set, it’s executed before the printing of each primary prompt ($PS1)
history
  • -a: Append the new history lines to the history file
  • -c: Clear the history list.
  • -r: Read the current history file and append its contents to the history list.
  • -n: Append the history lines not already read from the history file to the current history list

Use one command per line

  • shopt -s cmdhist: Store multi-line commands in one history entry

Add date and time to bash history

  • HISTTIMEFORMAT="%h %d %H:%M:%S "

HISTCONTROL: how commands are saved

  • HISTCONTROL=ignorespace:erasedups | | | |:———– |:—————————————————– | | ignorespace | don’t save lines which begin with a character | | ignoredups | don’t save lines matching the previous history entry | | ignoreboth | use both ‘ignorespace’ and ‘ignoredups’ | | erasedups | eliminate duplicates across the whole history |

HISTIGNORE: Ignore specific commands

  • HISTIGNORE="history"

Others setting about bash history

  • HISTFILE: change the history file name, the default value is ~/.bash_history.

History searching

  • with the below setting, we save bash history across all terminals.
  • Up/Down to scroll back and forward
  • ^R: search the typed substring in all history entries
  • history | grep xx: quickly search and find commands we ran before.

The Complete Setting for Bash History

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)