Successfully Save The Day With Bash History Commands

Published:29 April 2022 - 6 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Have you somehow felt you ran commands you’re not supposed to? Did they mess things up and leave you baffled about what happened? With Bash history commands, you save yourself the headaches of figuring out what messed what and save time rerunning complex commands.

Not a reader? Watch this related video tutorial!
Not seeing the video? Make sure your ad blocker is disabled.

In this tutorial, you’ll learn some of the most helpful Bash history commands and never lose track of your commands’ executions.

Read on and let Bash history save the day!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following.

  • A Linux machine – This tutorial uses Ubuntu 20.04, but any Linux distribution will work.
  • A user account with root privileges.

Viewing the Bash History

Most Linux distributions use Bash as the default shell. Like other shell environments, Bash maintains a record of the commands you’ve executed previously. This record is kept and maintained in a file called ~/.bash_history even if you restart your system.

But how do you view Bash history? By running the history command on its own or piped with another command.

1. Run the history command below to display the history of commands.

history

Below, you can see the entire list of commands executed by the root user in the current shell session. By default, the commands are displayed in the list from oldest to newest (top to bottom).

The below output shows just a small fragment of the entire history file. In many cases, you’ll get thousands of history lines, and you’ll have a hard time scrolling through them. But read on, and you’ll learn to customize the output in the way you want in the following steps.

Showing Bash History of All Executed Commands
Showing Bash History of All Executed Commands

2. Next, run the history command below to view the last 10 commands recorded in the Bash history. Replace 10 with the number of lines you like to display.

history 10

As you can see below, the output is much more manageable.

Showing the Last Ten Executed Commands
Showing the Last Ten Executed Commands

3. Lastly, run the following command piped with the head command to list only the first ten (oldest) commands executed by the root user in the current session.

Like in step two, replace 10 with the number of lines you like to display.

history 10 | head

As shown below, viewing the oldest commands is useful when checking commands that have been executed at the start of a session.

Listing Ten Oldest Commands Executed in the Current Session
Listing Ten Oldest Commands Executed in the Current Session

Viewing Bash History of Commands by Date and Time

The history command is handy but doesn’t show you the date and time you executed the commands. The date and time information can be beneficial in troubleshooting or finding the cause of an issue.

Fortunately, you can get this information by setting the date and time format of the Bash history in the HISTTIMEFORMAT variable.

Run the below command to set the date (%F) and time (%T) format of the Bash history, where:

  • The %F option is for the full date, shown in year-month-day format.
  • The %T option is for the time, shown in hour:minute:second (24-hour) format. Note that you’ll need to add a space after the %T option to separate it from the commands.
HISTTIMEFORMAT='%F, %T '

You can use any combination of the format options. For example, if you only like to show the time you ran the commands, use %T. Or use the %F option If you only want to show the date instead.

Now, run the history command alone to view the Bash history.

history

As you can see below, the output now includes the date and time when the commands were executed.

Viewing Bash History with Date and Time Commands Were Executed
Viewing Bash History with Date and Time Commands Were Executed

Searching the Bash History

You’ve seen how to view the Bash history and control the number of commands to view. But what if you like to find a particular command in your Bash history and don’t remember exactly what it was? Piping other commands, like grep, to the history command will do the trick.

Run the command below to collect (grep) each line of commands in the Bash history that contains the ls text, and print the last five (5) commands. The -i option tells grep to ignore letter cases of each line of command.

Be sure to replace ls with the word or phrase you want to search for.

By default, the grep command handles case sensitivity, showing the distinction between letter cases in the file. For example, If you search for “ls,” you’ll see commands with “ls,” but not “LS” nor “Ls.”.

history 5 | grep -i ls

You can see below that the last five commands with ” ls “ are displayed, regardless of the letter case.

Showing the Last Five Commands Containing “ls” and Ignoring the Letter Case
Showing the Last Five Commands Containing “ls” and Ignoring the Letter Case

Executing Commands from Bash History with Event Designators

You already know how to view commands from your Bash history in different outputs. But did you know you can also run commands from your Bash history without retyping everything?

This feature saves time when you run the same commands multiple times using event designators. Event designators allow you to reference a command by its position in the history list and are useful when rerunning complex commands.

To execute a command from your Bash history, you’ll type an exclamation point (!) followed by the number of the command to execute.

1. View the Bash history with any methods you’ve learned so far. But tutorial runs the history command without options or piped commands.

history

The numbers in the left column in the output below are the command history numbers called event designators.

Note the number of the command you like to rerun from the Bash history, in this case, the apt install sudo command.

Viewing Bash History
Viewing Bash History

2. Next, run the following command to execute the 11th (!11) command from your Bash history

!11

You can see below that you execute the apt install sudo command by running the !11 command.

Executing the 11th Command from the Bash History
Executing the 11th Command from the Bash History

Perhaps you like to dry run the command before actually running it. If so, append the :p option to any of your target commands, as shown below. This option is helpful if you don’t want to mess things up if you rerun a command.

!11:p

Notice below the !11:p command prints out the target command from the Bash history without running it.

Expanding Commands without Executing
Expanding Commands without Executing

3. Run the following command (!!) if you perhaps want to execute the last command in the Bash history. This command is helpful when you accidentally exit out of a command or forget to run a with sudo privileges.

!!

You can see below the !! command runs the last command in the Bash history, the history 5 command.

Executing the Last Command
Executing the Last Command

4. Now, run the below command to rerun a command prepended with sudo, perhaps the touch command, from the Bash history. If you forgot to prepend the sudo in a previous command, this feature is helpful if you’re using a non-root user.

sudo !!
Prepending sudo to the Last Command
Prepending sudo to the Last Command

5. Lastly, run the commands below to set the ping command as the last command and rerun the last command in the Bash history by specifying the command’s name.

This feature is helpful if you remember the beginning of the command’s name, so you don’t have to view and search the Bash history.

# Ping google.com
ping google.com
# Rerun the specified command (ping)
!ping

You can see below the !ping command finds the most recent command that starts with ping and reruns that command.

Rerunning Command by Specified Part of Command Name
Rerunning Command by Specified Part of Command Name

Reusing Arguments from the Bash History

Rerunning commands from history without retyping everything is fun, right? But what if you like to run a new command while using arguments from another command? In this case, using the !$ event designator comes in handy to designate the last argument of the preceding command.

Like the previous examples, !$ lets you reuse long or complicated arguments without retyping them. For example, you create a new file with a long path and file name, and you want to use the same name for another file. So you don’t have to type it out again. You can do this by typing the following command.

Run the below commands to create a file with a long path and file name and open the file on a text editor without retyping the entire file path.

# Creates a file called this_is_a_long_file_name
touch /home/ata/Documents/this_is_a_long_file_name
# Expand to the last command's arguments and 
# opens the this_is_a_long_file_name file in a text editor
nano !$

As you can see below, using the !$ event designator saves you a lot of time rerunning commands with lengthy or complicated arguments.

Reusing Previous Command Arguments from Bash History
Reusing Previous Command Arguments from Bash History

Conclusion

In this article, you’ve learned how to use Bash history commands to save time and improve productivity. At this point, you already know how to view a certain number of commands, search for specific commands, rerun commands, and reuse arguments from your Bash history.

These skills will come in handy when working on the command line. So why not learn Bash shortcuts to build up these skills and boost your productivity in a command-line environment?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!