(One)-minute geek news

Group talks, Laboratoire de Chimie, ENS de Lyon

Bash history


With autocompletion, the bash history is one of the features that contribute the most to the efficiency of Bash.

Let us investigate this feature, which possesses some hidden gems despite its apparent triviality.

The basics

In a conventional Bash terminal, the last commands are saved on the disk in a file named by default .bash_history in the home directory of the user.

It is possible to navigate through this history with the Up and Down arrows, allowing the user to re-edit, and re-execute previous commands.

Any command starting with a space will not be included in the history (by default).

Be careful, usually the commands executed in a terminal are saved in the history when this terminal is closed (problematic for simultaneous bash sessions)!

Searching in the history

The whole history can be displayed with the command history. Therefore, a simple yet powerful research can be performed with the mythic combo: history|grep 'pattern'

Actually, I strongly encourage you to define the following alias in your ~/.bashrc configuration file: alias "hgrep=history|grep"

There are numerous shortcut commands for searching through the command history. Let us mention Ctrl-r which performs an iterative search through your history.

Finally, let us mention that there is an autocompletion feature specific to the history! To autocomplete a word from the history, use Esc-Tab the same way you would use Tab.

Directly playing with the history

To directly re-execute the last command starting with something, simply execute !something

Example: To re-submit the last job, execute: !qsub

You might want to check that command and eventually edit it before its execution. In that case, to simply print the last command before the execution, use the !something:p syntax

Imagine, that you are typing a command, and in the middle of that command you need to check something by executing another command. First you would need to save the current command to re-edit it after your check. You can do exactly that by pressing Esc-# which will save your command as a comment. Smart isn't it?

You can paste the last argument with Esc-., quite useful for changing the editor, ...

Last but not least, when you want to re-execute a series of successive commands, simply go to the first command, and press Ctrl-o to execute the command and directly go to the next command in the chronological order! Try it on an example, and you will see the power of this trick!

Setting your .bashrc

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"