Working With Your BASH History
One of the common complaints from new Linux server users is of the complexity of working at the command line. This can cause great confusion as many are more familiar with the GUI paradigm, where you can often find the answer for what you need to do by starting applications and hunting through menu options. By contrast, the command line feels cold and unhelpful with its blank screen and flashing cursor. Fortunately there is some helpful advice to make dealing with the command line easier. In this article we’ll be looking at the BASH history.
What is BASH?
BASH, or the Bourne Again SHell is the default command line shell for most Linux distributions. This means that it’s the interface that most Linux users are familiar with when they think of the command line. One of the more helpful features of BASH is that it stores a history of all of the commands that each user runs on the system. This is kept by default in the user’s home directory in a file named .bash_history.
The .bash_history file is a plain text file that is simply a list of all of the commands a user has run on the server in the order they used them. By default the file’s permissions only allows the owner to access it, although another user’s history can be read by the root user. The beauty of this system is that when you are working at the command line and want to perform a task you have done before, or refresh your memory as to how you have used a command previously, you can search the history to see what you did before. For users that don’t regularly work at the command line this can help save a lot of time as once you’ve figured out how to do something once you can repeat the process later.
Something worth noting is that BASH saves the user’s history when the user logs out from their session. So, if that session is interrupted by a network disconnection then the history from that session will be lost. This also means that should you be working on two terminal sessions to the same server simultaneously then you won’t see one session’s bash history in the other.
So how do you make use of this history? The easiest method is what is known as “reverse-i-search”. To activate it, press ctrl – r and the command prompt will change to show the activation of reverse-i-search mode. Now if you start typing, the command prompt will show what you typed, and then the most recent command you used with what you typed in it. Pressing ctrl – r again will then take you to the next previous matching command, and so on as you repeatedly press that key combination. Once you have found the command you are looking for, you can hit the enter key to execute that command straight away. Alternatively, pressing the right cursor key will cancel the search, but will leave the command in place so that you can edit the command before executing it.
Another way of searching through your history is to use the history command:
history
As you can see from the output of this command, it gives a numbered list of the commands you have previously used. As with other commands, you can pass the output of history through commands like grep to filter through the output:
history | grep nano
The line above will display the commands from your history that have included the text “nano”. If you want to repeat a command from the history list then you can use the line number as a shorthand for it prefixed by an exclamation mark:
!24
This will execute the command from line number 24 of the history. This works by BASH converting the !24 to the relevant command when it is executed, meaning that you can use it as a shorthand to include a previously used command with other commands – for example:
cat /var/log/mylogfile | !32
This command will pipe the output from catting the /var/log/mylogfile file to the 32nd command from your history file. Another handy shortcut is the double exclamation mark “!!”. This will execute the previously typed command, and can be helpful in situations where you’ve typed a command, then realised you needed to use sudo to escalate your privilege level:
sudo !!
The above command will execute your previously typed command, but prefixed with the sudo command.
That just about covers most of the basic but very helpful commands for working with your BASH history at the command line. This should help you find and reuse commands you’ve used before, and can also save you time by reducing the need to fully retype commands you’ve used before.