The history command in Unix-like operating systems such as Linux and macOS is used to display the command history of a terminal session. This command is particularly useful in the Bash shell, where it's often employed to view, navigate, and manipulate the list of previously executed commands.
Each command executed is treated as an event and associated with an event number. These commands are saved in a history file (typically ~/.bash_history for the Bash shell, ~/.zsh_history for Zsh).
As commands are executed in the active shell, they are added to the history in RAM, not directly appended to any file. It is only appended to the file when the bash terminal is closed (or unless -a option is used).
- Recalling complex commands.
- Repeating previous commands.
- Identifying actions taken on a system at a specific time.
Here's the basic syntax of history command:
Here's a breakdown of the possible options and how
n can be used with them:
-c: Clears the entire command history.
-doffset: Deletes the history entry at a specific position offset.
-a: Appends the current session to the history file.
-n: Reads all history lines not already read from the history file into the current session's history.
-r: Reads the history file and appends its contents to the current session's history.
-w: Replace the content of the history file with your current session history.
-p arg [arg ...]: Performs history substitution on the following arguments and displays the result. It doesn’t save the result to the history list.
-s arg [arg ...]: Adds the arguments as a single entry in the history list.
To use the history command, simply type "history" in the terminal.
This pulls the commands from this file combined with the commands from the current session. It first displays the commands in the history file followed by commands you've run in the current session.
However, to show a limited number of previously executed commands. For example, "history 10" will display the last 10 commands in the history.
For Bash, the history is usually stored in ~/.bash_history. You can view all its content using the cat command:
However, be aware that the number of commands saved to this file is typically governed by the HISTFILESIZE variable. If this variable has been set to a particular value, only that many of the most recent commands will be saved to the file.
When working with multiple terminal windows or sessions open simultaneously, the command histories from these sessions can overwrite each other. In those cases append immediately after command executed, use PROMPT_COMMAND.
In the Bash shell, the behavior of the history command and command-line history functionality is influenced by several environment variables. Here are the most significant history-related variables:
This variable specifies the name of the file in which command history is saved. The default value is ~/.bash_history. To view the current HISTFILE path type
echo $HISTFILE from the terminal. However, you can change the location by setting the HISTFILE variable to a different path. For example:
HISTFILESIZE and HISTSIZE
The HISTFILESIZE environment variable in Bash determines the maximum number of lines or commands that the history file can have. Whereas HISTSIZE variable determines the number of commands that the shell remembers in the command history for the current session. When the Bash session ends (for instance, when you close the terminal or exit the session), the commands from that session's in-memory history are appended to the HISTFILE.
You can change the values:
export HISTSIZE=1500 export HISTFILESIZE=3000
This will allow the history of the current session to store up to 1500 commands and the history file to store up to 3000 commands. This change defaults 1000 and 2000 on the Ubuntu machine.
You can add lines to your ~/.bashrc or ~/.bash_profile file to make the change persistent across sessions.
You can also configure it to display timestamps alongside each command.
To display timestamps with your command history, you need to set the HISTTIMEFORMAT environment variable.
export HISTTIMEFORMAT="%d/%m/%y %T "
713 19/09/23 11:38:21 echo This command is added to history but not executed 714 19/09/23 11:38:23 history 715 19/09/23 11:39:30 mkdir working 716 19/09/23 11:39:34 history 717 19/09/23 23:44:19 export HISTTIMEFORMAT="%d/%m/%y %T " 718 19/09/23 23:44:24 history
In this example, the format
"%d/%m/%y %T " will display the date as
dd/mm/yy and the time in 24-hour format (HH:MM:SS). Once is set, use the history command as usual.
This variable uses formatting strings that are based on those used by the strftime function. You can customize the timestamp format in command history using function specifiers.
Controlling History Behavior
The HISTCONTROL variable in Bash provides a way to control how commands are saved in the command history. The possible values (and combinations thereof) for are:
- ignorespace: Commands that start with a space character will not be saved to the history.
- ignoredups: If you run the same command multiple times consecutively, only one instance of the command will be saved to the history, and consecutive duplicates will be ignored.
- ignoreboth: This is a shorthand way of setting both ignorespace and ignoredups
- erasedups: When a new command is added to the history, all previous occurrences of that command will be removed, ensuring that only the latest occurrence is retained in the history.
Example: To set ignorespace
Another way to control history behavior is to use HISTIGNORE variable. It allows you to specify a list of commands that you want to be excluded from the command history.
Commands like ls, ls -l, ls -a etc, pwd and exit won't be saved in the history.
Do you know you can use -s option to add a specific command to the current session's history without executing it. Example:
history -s "mkdir dir1"
This adds the command "mkdir dir1" to the history list. This means if you then press the up arrow key immediately after issuing history -s, you'll see
mkdir dir1 as the command, just as if you had typed it but hadn't pressed Enter yet.
Deleting specific commands from history
First, view your command history to identify the number associated with the command you wish to delete. Once you identified use -d <number> to delete it.
history -d 695
Clearing the entire command history
Use -c option to clear the history for the current session
Previous sessions are stored in a file (usually ~/.bash_history), delete that file that will remove the history file.
You can use unset HISTFILE command if you don't want commands from your current session to be saved to the history file when you exit.
An alternative method is to set HISTSIZE to 0, this won't save any new commands (from the current session) to the HISTFILE because no commands were stored in memory. The pre-existing commands in the HISTFILE remain unchanged unless you make modifications to the file itself.
You can use grep in conjunction with the history command to search for specific terms.
history | grep 'mkdir'
This searches for all occurrences of mkdir command in the history.
You can also combine with tools like head and tail, you can extract specific portions of your command history. To see only the last N lines of history, we can use history | tail -N. This provides a concise view of recent commands.
Alternatively, you can make sure of interactive search using reverse search (Ctrl + r) or forward search (Ctrl + s). Note: On some terminals, Ctrl + s might freeze the terminal (it's the command to stop output to the terminal). You can use Ctrl + q to unfreeze it.
- Ctrl + P: Move to the previous command in the history.
- Ctrl + N: Move to the next command in the history.
Executing Commands from History
One of the advantages of using the history command is its ability to reuse previously executed commands.
Using !! (Double Bang):
This refers to the last command. It's useful for quickly repeating the previous command.
$ ls $ !! # This will re-execute 'ls'
Using ! (Bang) with a Number:
You can execute a command from the history by referencing its number.
$ history 100 ls 101 ls -l $ !100 # This will re-execute 'ls'
This is to execute command number 100.