Ubuntu / Linux news and application reviews.

The Bourne Again Shell, better known as bash, is the default for most Linux distributions. It’s extremely powerful as far as command shells go, and holds all kinds of nifty tricks for those willing to learn. One of the best features is, in my opinion, the command history system.

There are many options to use with the bash history, some of them I use nearly every time I open a command shell. Today I’ll be covering some of the most useful aspects of the history, but I certainly won’t be able to cover them all.

Up/Down Arrows

Many of you probably know this one, but for anyone who doesn’t, it’ll make your life a lot easier. Any time you’re in the command shell, simply use the up and down arrows on your keyboard to move through the list of previously entered commands. Next time you enter a long command that turns out to have a typo, you don’t have to retype the whole thing just hit the up arrow and make whatever edits are needed.

!!

A similar feature to the one listed above is the double bang (!!). Those characters entered in the shell represent the last typed command. Let’s say you’re trying to run a command only to find out that you need root privileges to do it. Instead of retyping the whole command, you can simply enter

sudo !!

Bash will substitute the !! for the previous command, as in the example below.

bash-history-bangbang

This also works further backward, you can do something like

!-5

to recall the command 5 entries back in the history.

!(word)

Perhaps a few days ago you typed a long, complex command into your shell, for example a series of options for “wterm”. You can find and re-enter than command by using the history’s built in search. You simply add the first few letters of the command after the ! and bash will find it.

Example of !(word)

Be careful with this one. You may end up running something other than what you expected. Don’t use this for potentially dangerous commands like “rm” or “fdisk”.

!?(word)

This is similar to the last feature in that it searches the history for the word entered, but unlike !(word) this will find the word anywhere in the command, not just the beginning.

bash-history-bangquestion

Be careful with this one. You may end up running something other than what you expected. Don’t use this for potentially dangerous commands like “rm” or “fdisk”.

Ctrl-R

This one may be my personal favorite history tool. It’s a bit like the !? item above, but interactive. In your command shell, hit Ctrl-R, and it will begin a search. As you type, bash will search the history and show you the results as you type. When it shows the command you want, simply hit enter and it will run that command. This can be safer than things like !? because you can see what the command will be before you run it, so you don’t have to guess or rely on memory.

Example of Ctrl-R in the shell

Manual Search

Perhaps you don’t like the way Ctrl-R works, or you want to see ALL the commands you typed, or just the ones that contain a particular word. The history command will show you your full command history, along with the history number of each command (hold that thought, we’ll cover those numbers in a moment).

You can also specify the number of items the history command will show. To see the last 10 entries in the history, you could type

history 10

To see all history entries that contain a particular word, you can use grep to filter the results, like below.

Example of using grep to filter history list

!(number)

When you use the history command to view your command history list, you’ll notice each item in the list has a number associated with it. You can use that to recall that particular command. For example, item 87 could be run again by entering

!87

into your shell.

Word replacement

This one’s pretty great. How many times have you written out some big long command only to realize you put, say, hdd when you wanted hdc? Well bash has you covered. You can replace a word in the previous command with another using “^”, as in the example below

Example of word replacement with "^"

Managing Your History

By default, the history is saved in ~/.bash_history. You can deal with this file however you see fit, but there are some useful things you could try to manage it automatically. Namely, setting the environment variables HISTSIZE and HISTFILESIZE. To set the number of entries that can be saved in the history file to 1000, enter

export HISTFILESIZE=1000

into your shell. This will limit the file to 1000 entries. If you want to make this change permanent, put that export command into your .bashrc or .bash_profile file.

[via maketecheasier]