Re-execute command line with !

Bash history expansion allows you to quickly re-run previous commands using ! and the number of the command in your history.

method/1492/Screen Shot 2012-04-22 at 16.03.03.png

You can adjust the size of the history by setting the HISTSIZE variable in the shell e.g. export HISTSIZE=1000. You can clear the current history with history -c.

Method

Bash keeps a history of all commands executed in the shell and this can be referenced using the ! command. Typing two !'s at the prompt will execute the previous command:

$ echo "HELLO"
HELLO
$ !!
echo "HELLO"
HELLO

The history of the bash shell can be accessed by entering history

501  echo "HELLO"
502  echo "HELLO"
503  history

Notice that each entry has an associated number which can be used to refer to the command.

Entering ! followed by a line number from the history will run that command

$ !501
echo "HELLO"
HELLO

You can also search the history by using a search string to match the command instead of a number

$ !echo
echo "HELLO"
HELLO

This method is based, with permission, on an original protocol available here.

Latest features

Scroll pHAT Tetris  raspberry-pi

The Scroll pHAT is a little 11x5 (55) white LED matrix pHAT which you can control easily from a Raspberry Pi. In this project we'll squeeze a tiny-yet-playable game of Tetris onto the Scroll pHAT. More

Find all files containing a given string  bash

A quick one-liner to recursively search files for a given text string. Open up a terminal session and enter the following - replacing "foo" with the text to search for. find . -exec grep -l "foo" {} \; You can also limit the search to files with a particular extension (e.g. HTML or ... More

Discussion

comments powered by Disqus