Bash command history is a valuable feature that enables users to recall and reuse previously executed commands. This functionality enhances productivity and streamlines workflow in the Bash shell.
When you run commands in Bash, they are automatically saved in a history file. By default, this file is located at ~/.bash_history
. The history feature allows you to:
There are several ways to interact with your command history:
The history
command displays a numbered list of recent commands:
$ history
1 ls -l
2 cd Documents
3 grep "example" file.txt
4 sudo apt update
Bash provides convenient keyboard shortcuts for navigating through command history:
Bash uses special characters called event designators to reference previous commands:
!!
: Repeat the last command!n
: Repeat command number n from history!string
: Repeat the most recent command starting with "string"You can customize how Bash handles command history using various options and environment variables:
These variables control the number of commands stored in memory and in the history file, respectively:
export HISTSIZE=1000
export HISTFILESIZE=2000
This variable determines which commands are saved in the history:
export HISTCONTROL=ignoredups:erasedups
This setting ignores duplicate commands and removes older duplicates from the history.
Mastering Bash command history can significantly improve your efficiency in the terminal. By leveraging this feature, you can reduce typing, avoid errors, and quickly access your most-used commands. As you become more familiar with command history, you'll find it an indispensable tool in your Bash command-line interface workflow.