Understanding how to navigate and utilize the command line history in Bash can significantly enhance productivity and efficiency for both novice and experienced users. The command line history serves as a repository of previously executed commands, allowing users to recall, modify, and reuse commands with ease.
The following are several different ways in which commands from the Bash command line history can be accessed and utilized:
Using the history
command
Using the !!
command
Using Ctrl-R
Using arrow keys
Let’s discuss each of the above-mentioned methods in detail.
history
commandOne way to access the history of the bash command line is to use the history
command. When entered into the command line, the console shows us the current history of the commands used. The history buffer can store up to 1000 command entries.
We can specify the number of commands we would like to see by using the command history n
, where n
is any arbitrary number. This command would return the n
most recent commands present in the buffer.
While displaying the commands to the command line, each command is also assigned an index in chronological order, with the very first command assigned the index 1
.
We can see the use of the history
command in action by running the below terminal:
!!
commandAnother way to access the command history in the Bash terminal is through the use of the !!
command. The !!
command is equivalent to the history
command, which displays the entire history of the terminal.
We can further modify the !!
command to get different outputs. As we discussed above, each command in the history buffer is assigned an index. We can specify the command that we want to use by replacing the second !
with the index number. For example, in the terminal above, the command echo hello world
is located at the index number 5
. If we enter the command !5
in the terminal, it will fetch and execute the echo hello world
command located at the index 5
, producing the following output:
root@educative:/# !5echo hello worldhello world
Similarly, we can replace the second !
with a specific command, such as !command
, where command
refers to any previously executed command stored in the history. This will execute the most recent instance of that command present in the history. For example, in the terminal above, executing !echo
will run the most recent echo
command present in the history.
Note: It is important to note that while the
history
command is also appended to the history itself, using the!!
command does not append the executed command into the history. The!!
command simply fetches the required command from the history and executes it.
Another way to access the history is through the keyboard keys “Ctrl+R.” If we press these keys within the terminal, we can expect to see the following output:
(reverse-i-search)`':
Once we see the above, we can start searching for any keywords we want, and the terminal will display the most recent command containing those keywords. Pressing the “Enter” key will execute that command. Similarly, if we want an instance other than the most recent one, we can press “Ctrl+R” as many times as required. With each press of these keys, the terminal will show older commands.
One of the easiest and most commonly known ways to use the Bash command line history is to use the arrow keys. Pressing the “Upward” key takes us to an older command, and pressing the “Downward” key takes us to a more recent one. If there is no older or more recent command, pressing the respective key does not make any difference.
Free Resources