Command History

LinuxBeginner
Practice Now

Introduction

Have you ever visited a website, closed the tab, and then realized you needed to go back? You probably used your browser's History.

The Linux terminal has the same feature! Linux saves your commands in a logbook called Command History.

This feature matters because shell work is often repetitive. History is not just a convenience for laziness; it is a practical way to reduce typing, avoid mistakes, and reuse commands that already worked.

In this lab, you will learn how to access this logbook, replay your recent actions, and search through your past.

View Command History

In this step, you will look at your command logbook. The shell records your commands in a list.

First, create some history by running these commands:

ls
pwd
whoami

Now, see the record:

history

You should see a numbered list of everything you typed:

    1  ls
    2  pwd
    3  whoami

This is your session receipt.

Those numbers are useful because history is not just a diary; it is also an index. Shells can use these stored commands as things you can recall, repeat, or search.

To see only the last 3 items:

history -3

Repeat Last Command (!!)

In this step, you will use Linux's "Repeat" button: !! (pronounced "bang-bang").

It recalls the very last command.

Check the date:

date

Now, instead of typing date again:

!!

The terminal expands !! to date so that you can review it. Press Enter again to run the expanded command.

This is an example of history expansion. The shell interprets a shorthand reference to an earlier command and replaces it before execution.

Try with echo:

echo "Hello LabEx"
!!

After !! expands, press Enter again to run the recalled echo command. This is a quick way to say "Do that again" while still reviewing the command before it runs.

In this step, you will find an earlier command without scrolling through the history list.

Use Reverse Search (Ctrl+R). It's like "Find on Page".

First, run two commands for testing:

grep "root" /etc/passwd
grep "bin" /etc/passwd

Now, let's find the first grep "root" command.

Press Ctrl+R. The prompt changes to (reverse-i-search) or bck-i-search.

Start typing grep. The terminal shows the most recent match (grep "bin"...). Press Ctrl+R again to cycle back to the previous match (grep "root"...), then press Enter to run it.

This is how experts move fast. They don't type; they search.

That is an important mindset shift. Efficient terminal use is not about memorizing and retyping everything from scratch. It is about using the shell's built-in tools to recall and adapt what you have already done.

Summary

In this lab, you learned that you never have to rely solely on your memory. Linux remembers for you.

  • history: Your logbook of past actions.
  • !!: The "Repeat" button for the last command.
  • Ctrl+R: The "Search" function to find old commands instantly.

Using these tools makes you faster and reduces typing errors. Why type a long command twice when you can just look it up?