#1: Last commands on Bash

Posted by | Comment (1) | Trackbacks (5)

You probably know the bash history function. All commands you entered are stored in memory and will be written to ~/.bash_history as soon as you exit your current shell. You may have used this often by tapping the up and down arrow keys to repeat the last few commands. But did you know that bash can do much more history work for you?

Bash has some really advanced history functionality implemented. For instance you can type an exclamation point followed by any number to repeat a specific command. To see all recently executed commands and their numbers run history. For instance:

$ history
  501  foo
  502  bar
  503  history

You can now e.g. type !502, to call the command bar again. But you don't have to look at this command list each time. You can also count upwards from the current command line by prepending a minus sign to the number. That is, !-3 calls the third last command again. Instead of !-1 which re-executes the last command you can also write !!.

But there's even more. You cannot only refer to specific numbers but also to the commands themselves. For example

!man

would recall the last command which starts with man. You can also look for commands which contain this string somewhere by running

!?man?

This searches for the last command containing man. The trailing question mark may be omitted if you want to search for commands, which end with that string.

Another useful feature is to re-run commands but replacing some of their contents.

^search^replace^

would run the last command again but replace search with replace.

Even more interesting are modifiers. With modifiers you can extend your event designators (i.e. the exclamation point expression). A modifier is added by appending a colon and the modifier itself. One modifier that is very handy is the search and replace modifier :s. To run the third last command but replacing the first occurrence of man with info run

!-3:s/man/info/

You see that ^search^replace^ is only a shorthand for !-1:s/search/replace/ and !!:s/search/replace/, respectively. To repeat the last replacement you may use the modifier :&.

There are plenties of other modifiers and also so-called word designators which can be used to refer to specific parts of a command. I can't cover them all in this blog post but have a look at the man page for history(3).

One last thing to mention: if you don't want some commands to be added to your history just add them to your HISTIGNORE environment variable. This variable contains a colon (:) separated list of all commands to be excluded from the history. And of course don't forget to clean up your history from time to time or lower the maximum number of stored commands by changing the value of the HISTSIZE environment variable.

Read more about bash history functions:

Trackbacks

robo47 sent a Trackback on : (permalink)

RT @reflinux: So, first article has been published: Last commands on Bash http://bit.ly/eRMbJN

Manko10 sent a Trackback on : (permalink)

RT @reflinux: #Advent series “24 Short Linux Hints”, day 1: Last commands on Bash http://bit.ly/hmIsYK

robo47 sent a Trackback on : (permalink)

RT @reflinux: #Advent series “24 Short Linux Hints”, day 1: Last commands on Bash http://bit.ly/hmIsYK

fraherm sent a Trackback on : (permalink)

♺ @reflinux: #Advent series “24 Short Linux Hints”, day 1: Last commands on Bash http://bit.ly/hmIsYK

Refining Linux sent a Trackback on : (permalink)

grep is a handy tool and I use it every day. But sometimes I wish it had some more features. For instance full featured Perl compatible regular expressions ( PCRE ) since the current implementation is not complete and I think it will never be, automatic rec

Comments

There have been 1 comments submitted yet. Add one as well!
franky
franky wrote on : (permalink)

Thanks! Hopefully I can improve my workflow with your hints.

Write a comment:

HTML-Tags will be converted to Entities.
Textile-formatting allowed
Standard emoticons like :-) and ;-) are converted to images.
Design and Code Copyright © 2010-2017 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms. tweetbackcheck