bash history - ghdrako/doc_snipets GitHub Wiki

Bash history keyboard shortcut Description
↑ key Scroll backwards through bash history
↓ key Scroll forward through bash history
ctrl+R Search for commands in your bash history
ctrl+O Run a command you found using a ctrl+R search
ctrl+G Exit a crtl+R search

Searching through shell history

  1. Press CTRL + R to invoke reverse-i-search.
  2. Type a part of the command you are looking for.
  3. Your shell will try to match the characters you type against your command history and come up with the closest, most recent match.
  4. Repeatedly press CTRL + R to step through the history. Press ENTER to select a command or Esc to exit this mode.
  5. If you accidentally skip backward past the command you wanted, CTRL + SHIFT + R will search forward to the next-most-recent match.

Prepending a command to something in your history

The !! command will execute the last command you ran, but with some other command in front of it.

apt-get install nginx # fails with a permission error
sudo !!
# this is the command that runs:
sudo apt-get install nginx

After that previous command fails due to a lack of permissions, simply running sudo !! will rerun it with sudo prepended to the beginning

Jumping to the beginning or end of the current line
  • CTRL + A - jump to the beginning of a line when editing,

  • CTRL + E - jump back to the end of a line

  • history: This is a Bash command that allows you to view the history list of previously executed commands. Typing history in the terminal will display a list of commands, along with their line numbers.

  • !!: This shortcut executes the immediately previous com- mand in the history list. For example, typing !! will execute the command on the line immediately above the current command.

  • !n: This shortcut executes the command with the line number n in the history list. For example, typing !3 will execute the command on line 3 of the history list.

  • ^: This symbol allows you to replace part of a command from the history list. For example, typing ^old^new will replace the first occurrence of old with new in the most recent command.

  • !$: This shortcut references the last argument of the previous command. For example, if the previous command was ls /home/alice/Desktop, typing rm !$ would delete the file or directory listed as the last argument (/home/alice/Desktop in this case).

Clear Bash history

history -d N delete a specific entry where N is the number of the command or a range of numbers.

history -d 2-4 # deletes commands 2, 3, and 4 from our history
history -c     # clear the history buffer
cat /dev/null > ~/.bash_history # clear file

Configuring Bash history settings with .bashrc

  • HISTSIZE controls the maximum history kept in memory
  • HISTFILESIZE controls the maximum size of the history file that’s saved between shell sessions

If you want to increase the amount of history that Bash keeps, increase both of the preceding in your shell’s configuration file.

HISTSIZE

$ echo $HISTSIZE
1000
$ echo $HISTFILESIZE
1000

To change those limits, edit ~/.bashrc file and add:

HISTSIZE=20000
HISTFILESIZE=20000

HISTIGNORE

HISTIGNORE='sudo *':'echo w*' # ignore any sudo commands or any commands where echo is followed by a w

HISTCONTROL

  • ignoredups causes lines which match the previous history entry to not be saved.
  • erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved
  • ignorespace lines which begin with a space character are not saved in the history list
  • ignoreboth is shorthand for ignorespace and ignoredups
HISTCONTROL=erasedups  # not store duplicate history commands
HISTCONTROL=ignoreboth:erasedups
PROMPT_COMMAND='history -w'

To immediately persist commands to your ~/.bash_history file, you can add this to ~/.bashrc:

PROMPT_COMMAND='history -a'

HISTFILE

The default value is ~/.bash_history. If unset, the command history is not saved when a shell exits.

Unlimited history

.bashrc:

# 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"

adding current history

cat ~/.bash_history >>~/.bash_eternal_history