Interactive shells can keep a record of commands you enter. This lesson focuses on Bash, where the history builtin displays and manages that record. Other shells may use different shortcuts, files, or settings.
Command Line · Lesson 9
history
Learn how to inspect, search, reuse, and manage command history in Bash.
Viewing Bash History
Run history to display the current history list:
$ history
101 pwd
102 ls -la
103 cat notes.txt
Each line has a history number followed by the command.
Which Bash command displays the current numbered history list?
Reusing Previous Commands
Bash provides several shortcuts for recalling or immediately executing commands:
- Up Arrow: Recall earlier commands for review or editing.
!!: Expand to and execute the most recent command.- Run by number: Use
!102to run command number 102 from your history. - Run by prefix: Use
!catto run the most recent command that started withcat.
History expansion forms that begin with ! can run a command as soon as you press Enter. Inspect the match first when there is any doubt, especially before adding elevated privileges or operating on important files.
Which Bash history expansion repeats the most recently executed command?
Searching History Interactively
Press Ctrl+R to start a reverse incremental search, then type part of the command you want. Press Ctrl+R again to move to an older match.
Press Enter to execute the displayed match. If you want to review or edit it first, use an arrow key to place the command on the editing line instead.
You remember part of an earlier Bash command and want to find it interactively. What should you press first?
Managing the History List
The history builtin can modify or save the current list:
history -c: Clear the current in-memory history list.history -w: Write the current list to the configured history file, commonly~/.bash_history.history -d <offset>: Delete the entry at the given history position.
Examples:
$ history -d 101
$ history -w
Clearing the in-memory list does not by itself guarantee that older commands have disappeared from every file, backup, or other active shell. History behavior also depends on Bash settings and when sessions read or write their files.
Which command writes the current Bash history list to its configured history file?
Clearing the Display and Completing Names
Use clear when you want a fresh visible terminal area:
$ clear
This does not erase the Bash history list. Depending on the terminal, older display content may also remain available in scrollback.
Tab completion is another way to avoid retyping. Start a command, filename, or directory name and press Tab. Bash may complete an unambiguous match or show possible completions when more than one exists.
Command lines can be stored in history, so do not place passwords, tokens, or other secrets directly in commands when a safer input method is available.
You want to refresh the visible terminal without deleting the in-memory command history. Which command should you run?
Lesson complete
You finished history
You can now find and reuse Bash commands while managing history deliberately.
Display the current numbered history list.
Recall or expand a previous command carefully.
Search history interactively with
Ctrl+R.Delete, clear, or write history entries.
Distinguish command history from the terminal display.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account