Skip to content
Home » Search & Repeat Commands in Bash with History Command

Search & Repeat Commands in Bash with History Command

The bash history command is a powerful tool that allows users to keep track of their command-line activities, making it easier to repeat frequently used commands, save time, and avoid errors.

Prerequisites

Before diving into the bash history command, it’s essential to understand the basics of the command line. Additionally, it’s important to note that this article assumes that you’re using a Unix-based operating system, such as Linux or macOS.

How to Use the Bash History Command

The bash history command is available by default in most Unix-based operating systems, and it’s used to list the commands that you’ve entered in your current session. To access the command history, simply type “history” at the command prompt and press enter. You’ll see a list of commands that you’ve entered, along with their corresponding line numbers.

$ history
  1  ls
  2  pwd
  3  echo "Hello World"
  4  history

As you can see, the list of commands is displayed in the order that they were entered, with the most recent commands appearing at the bottom of the list. To repeat a command, simply type “! ” followed by the line number of the command you want to repeat. For example, to repeat the “echo” command from the example above, you would type “!3” and press enter.

$ !3
Hello World

Another way to repeat a command is to use the “! ” followed by the first few letters of the command. For example, to repeat the most recent “dig” command, you could type “!dig” and press enter.

 1000  dig google.com
 1001  dig -x google.com +trace
 1002  dig -x google.com +short
 1003  dig -x 172.17.0.1 +short
 1004  dig google.com MX +short
 1005  dig google.com +short
 1006  dig google.com +answer
 1007  history
itvraag@L5PRO:~$ !dig
dig google.com +answer

; <<>> DiG 9.18.1-1ubuntu1.3-Ubuntu <<>> google.com +answer
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 263
;; flags: qr rd ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;google.com.                    IN      A

;; ANSWER SECTION:
google.com.             0       IN      A       142.251.39.110

;; Query time: 10 msec
;; SERVER: 172.17.0.1#53(172.17.0.1) (UDP)
;; WHEN: Sat Feb 04 19:21:21 CET 2023
;; MSG SIZE  rcvd: 54

Searching the Bash History

In addition to repeating commands, you can also search your command history. This is especially useful when you can’t remember the exact command that you want to repeat. To search the history, simply type “ctrl + r” and then start typing the command that you’re looking for. The first matching command will appear, and you can cycle through the matches by pressing “ctrl + r” again. To execute the matching command, press enter.

(reverse-i-search)`l': ls

Customizing the Bash History

The bash history command is highly customizable, and you can change its behavior to suit your needs. One of the most important settings that you can adjust is the size of the history buffer. By default, the bash history buffer is set to 500 commands, but you can increase or decrease this value as needed. To change the size of the history buffer, you need to edit the “.bashrc” file in your home directory. Add the following line to the file:

export HISTSIZE=1000

This will increase the size of the history buffer to 1000 commands. To make the changes take effect, you need to log out and log back in, or run the following command:

$ source ~/.bashrc

Clearing the Bash History

There may be times when you want to clear your command history. This is useful when you want to keep your history private, or when you simply want to start fresh. To clear the bash history, you can use the “history” command with the “-c” option. This will clear the entire history buffer, so be sure to only use this option when necessary.

$ history -c

5 Tips for Using the Bash History Command

  1. Use the up and down arrows to quickly access previous commands. You can also use the “ctrl + p” and “ctrl + n” shortcuts to move through your command history.
  2. Use the “history” command with the “-d” option to delete specific commands from your history. For example, “history -d 5” will delete the 5th command from your history.
  3. Use the “history” command with the “-w” option to write your history to a file. This can be useful for backup purposes or if you want to transfer your history to another computer.
  4. Use the “history” command with the “-r” option to read a file of commands and add them to your history. This is useful when you want to import commands from another computer or when you want to reuse a set of commands that you’ve saved in a file.
  5. Use the “fc” command to edit and re-execute previous commands. This can be useful when you need to make a small change to a command that you’ve already entered.

Challenge

Try using the bash history command to repeat any of the most recent command that you entered. Then, try searching your history for a command that you recently entered, and repeat it using the reverse-i-search feature. Finally, try using the “fc” command to edit and re-execute a command from your history.

Next Steps

Now that you have a good understanding of the bash history command, you may be interested in learning about other advanced bash commands and features. Some recommended topics to explore next include:

Leave a Reply

Your email address will not be published. Required fields are marked *

19 − 10 =