Bash History

You can see which file bash history is being written to by doing:

  • echo "$HISTFILE"

As you probably already know, you can use the up and down arrow keys to cycle through the bash history, but you can also search it!

To do so, press C-r. You will be prompted for a search term. Start typing in part of the command you are trying to find. For example : if it was a mplayer? command, start typing mplayer. You will notice that as you type, the most recent command from the bash history matching what you have typed will be displayed to you. When you see the one you are after simply press enter to execute that command.

List Bash History Commands

Use the fc command with the -l switch:

  • fc -l -5

Will list the last 5 commands in the bash history file.

  • fc -l 5 10

Will list commands 5 to 10 in the bash history file.

  • fc -l mplayer mplayer

Will list the last mplayer command.

Command Substitution

!            Begin a history substitution.
!!           Previous command.
!N           Command number N in history list.
!-N          Nth command back from current command.
!string      Most recent command that starts with string.
!?string?    Most recent command that contains string.
!?string?%   Most recent command argument that contains string.
!$           Last argument of previous command.
!#           The current command up to this point.
!!string     Previous command, then append string.
!N string    Command N, then append string.