The shell maintains an active command line history. It is an amazing and helpful feature that keeps the track of all the commands that have been entered. Apart from keeping track of all the commands ever typed in in the terminal, we could also reuse those commands withouth retyping them from scratch (and risking typos!).
We can utilize the command line history feature by simply writing – you’ve guessed it – history
.
Additionally, we could also access our command history by checking the .bash_history
hidden file found in our home directory.
$ less .bash_history ... ls clear ~ ls clear cat .bash_history clear cat .bash_history less .bash_history clear
So why is the command line history useful? As mentioned it is a powerful resource for finding and reusing past commands, and it makes the command line workflow a lot more bearable.
The history command
The file itself is not that useful because it doesn’t provide us with mechanisms to analyze the contents in more detail. But the terminal command history
is here to the rescue!
$ history 7055 ls 7056 clear 7057 ~ 7058 ls 7059 clear 7060 cat .bash_history 7061 clear 7062 cat .bash_history 7063 less .bash_history 7064 clear
Look at the index on the left side – more than seven thousand commands! How can we possibly search for a command that we want to re-type? For that, we can combine the history
command and the grep
command. Let’s say I always forget how to run my Python files with a debugger when I want to debug my Python scripts. IThe only thing I remember is the keyword pudb
which is a flag that targets the debugger itself.
$ history | grep pudb 7047 python -m pudb index.py zen.txt 7049 python -m pudb index.py zen.txt
And look at that! The number on the left, as you’ve might guessed it is the line number of the command in the command line history. We can rerun this command by using history expansion that will expand a certain index into the command itself.
$ !7049 $ python -m pudb index.py zen.txt
The exclamation point combined with history expansion serves more uses, and here are some of them:
Sequence | Action |
---|---|
!! | Repeats the last entered command |
!number | Repeats the history idem with the appropriate id |
!string | Repeats last history item starting with string |
!?string | Repeats last history item containing string |
Instead of writing !!
for the last entered command, we could also press the arrow key up. We can look through the list of entered commands by using the arrow keys, and we can execute them by pressing Enter on our keyboard.
With !string
and !?string
we can search for a past command, although this wouldn’t be advisable due to the complexity of the history list.
History Commands
Now we will see some of the most common keystrokes used for history navigation. I already mentioned the arrow keys, and the way we can search through the history using grep
, but there is an additional way of looking through the history.
Sequence | Action |
---|---|
CTRL + r | Searches incrementally from the current command up the history list |
CTRL + p | Previous history entry. Same as the up arrow key. |
CTRL + n | Previous history entry. Same as the down arrow key. |
ALT + < | Moves to the top of the history list |
ALT + > | Moves to the bottom of the history list (to the current command) |
Final Words
Hope you learned something interesting regarding the history mechanism in bash, please check some more Linux articles by clicking down below.