Linux Basics: Reusing Bash Command History
Bash can remember commands entered in an interactive terminal. History helps you reuse an earlier command. These examples assume Bash on Debian or Ubuntu with history enabled.
List recent commands
Enter these commands one line at a time in the same terminal:
printf 'history practice\n'
pwd
history 10history 10 lists up to ten recent entries with their history numbers. Your list will depend on what you have run. The history command itself may also appear.
You can narrow a longer list to entries containing some text:
history | grep 'printf'This searches the displayed history without running the matching commands. The search command can appear in its own results.
Recall and review a command
With Bash’s usual keyboard bindings, press the Up arrow to recall the previous command. Keep pressing it to move through older entries, and use Down to move forward again.
Edit a recalled command before pressing Enter. Pay particular attention to paths, options, and commands that change files. A command that was useful yesterday may affect different files today.
Press Ctrl+R to search backward through history, then type a distinctive part of the command. Press Ctrl+R again to find an older match. Ctrl+C cancels the search; Enter runs the selected command.
Keep sensitive text out of history
Avoid putting passwords or tokens directly in commands. History may be saved to a file when the shell exits, and terminal logging can preserve text separately. History is a convenience, not a secure place for secrets.
Reference
Acknowledgements
This post was created with assistance from AI.
Linux Basics — lesson 3 of 25. Series index
Previous: Getting Help for Commands · Next: Creating, Copying, and Removing Files