The Command History
6 min read·Jan 1, 2025
Just like your web browser saves the searches you perform, the shell keeps an internal history of all the commands you execute, allowing you to easily re-run them in the future.
In Bash, the command history is saved in a text file named .bash_history and is located in your home directory, which is /home/username on Linux and /Users/username on macOS.
Browsing through the history
The easiest way to browse through the list of commands saved in your shell history is to use the UP and DOWN arrow keys of your keyboard, where:
- Pressing the
UPkey will make you browse the history backwards. - Pressing the
DOWNkey will make you browse the history forwards.
Once you've found the command you want to re-run, you can either:
-
Press the
ENTERkey to execute it. -
Press the
CTRL + Ckey combination to cancel this command and get a new prompt without having to erasing the whole line.
Listing the commands in the history
To display the complete list of all the commands saved in your shell history, you can use the history command without arguments:
$ history
Which will display a similar output:
$ history
1 cd Projects
2 ls -l
3 echo "Hello World"
Showing the last N entries
As the history list usually grows quickly in size, you can shorten the output by only displaying the last N entries using the following command:
$ history n
Where n is a number representing the last N entries.
Example
For example, this command will only display the last 10 entries:
$ history 10
Executing a command
To re-run one of these commands, you can use this command:
$ !n
Where n is the number associated with the command in the history list
Example
For example, this command will execute the 3rd command in the history:
$ history
1 cd Projects
2 ls -l
3 echo Hello World
$ !3
echo Hello World
Hello World
Executing the last command
To quickly re-run the last command saved in the history, you can use this command:
$ !!
Searching for commands
Rather than browsing through the entire history list, you can quickly find a command using the reverse search function.
To trigger the search menu, simply press the CTRL + R key combination:
(reverse-i-search)`':
And start typing the command you're looking for:
(reverse-i-search)`ech': echo "Hello World"
If the suggested command doesn't right away match your search, you can press CTRL + R again until you find it.
Finally, you can press the ENTER key to run the suggested command:
(reverse-i-search)`ech': echo "Hello World"
$ echo Hello World
Hello World
Or the CTRL + C key combination to close the search menu:
(reverse-i-search)`ech': echo "Hello World"
$
Note: The commands will appear in reverse order, from the newest to the oldest.
Summary
Here's a summary of what you've learned in this lesson:
- The
UPkey allows you to browse the history backwards. - The
DOWNkey allows you to browse the history forwards. - The
historycommand allows you to display the history list. - The
!nsyntax allows you to execute thencommand in the history list. - The
!!expression allows you to execute the last command in the history list. - The
CTRL + Rkey combination allows you to trigger the reverse search function. - The
CTRL + Ckey combination allows you discard the current command without having to erase it.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery