Shell History

Each command you enter into the shell is recorded in your shell history, having access to your shell history is extremely useful. You can search through your shell history, repeat commands that you've previously entered. And even recall previous commands, and change them slightly before executing them again. Not only can this save your time in keystrokes, but it can prevent you from making mistakes by running a previously known, good command.

Some shells like bash keep their history in memory, and only write the history to a file on exit. Common history files include .bash_history, .history and .histfile. These history files are stored in your home directory.

  • ~/.bash_history
  • ~/.history
  • ~/.histfile

The history command

The history command displays the commands in your shell history. It proceeds each one of the commands with a number, that can be used to reference that command at a later time. By default, bash retains 500 commands in your shell history. This is controlled by the HISTSIZE environment variable. If you want to increase this number, run export HISTSIZE=number and place that in your personal initialization files.

Commands Meaning
history Displays the shell history.
HISTSIZE Controls the number of commands to retains in history.
For example: retains 1500 commands
export HISTSIZE=1500

The ! syntax

You can use exclamation mark history expansion syntax to rerun a command by number. Run the history command to get a list of commands, that are preceded by a number. The, if you want to rerun command number N in your history, you would type !N.

Note: Sometimes the exclamation mark is abbreviated, or called bang. So, if people say "Bang 4", you know that means the !4.

If you want to repeat the previous command, you can use !!. If you want to repeat a command that start with a certain letter or string, you can use !string. For example, to repeat the cat command, you can simply enter !c.

Commands Meaning
!N Repeat command line number N.
!! Repeat the previous command line.
!string Repeat the most recent command starting with "string".

In addition to execute entire commands in your history, you can pull out parts of a command line. The syntax is

!:N <Event> <Separator> <Word> 

:N represents a word on the command line. 0 is the command, 1 is the first argument, etc.

Example:

Suppose you have run the following command:

$ tail files.txt sorted_files.txt notes.txt

Display previous command:

$ !!

To pull out the 2nd argument to previous command:

$ vi !:2

Above command is the same as vi sorted_files.txt.

In addition, there are 2 more exclamation marks syntax shortcuts.

Commands Meaning
!^ Represents the first argument, the same as !:1
!$ Represents the last argument.

The Tab completion

Another way to increase your efficiency at the command line is by using Tab completion. After you start typing a command, you can hit the Tab key to invoke tab completion. Tab attempts to automatically complete partially typed commands.

Examples

Example 1: Display command history

Display command history

$ history
  1  history

Display command history using !N

$ !1
  1  history
Example 2: Rerun last command

Rerun last command using !!.

$ echo 'hello world'
hello world
$ !!
echo 'hello world'
hello world

The !! displays the command that's going to be executed, run the command and returns the output from that command.

Example 3: Display history command with a specified string

Display commands that has 'h'

$ !h
history
  1  history
  2  echo 'hello world'

Then, if you want to run the echo 'hello world' command, you can use !2 .

Example 4: Pull out portions of a command

Suppose you have run the following command:

$ echo 'one' 'two' 'three' 'four'

And to pull out the 2nd argument, use !:2

$ echo !:2
echo 'two'
two

To pull out last argument, run !$

$ echo !$
echo 'three'
three

To pull out first argument, run!^

$ echo !^
echo 'one'
one

References & Resources

  • N/A