Linux / Command / history / Command to display a list of previously executed commands
In Linux, the history command in Bash displays the command history managed by the current shell along with numbers. You can check the history to find previous commands or re-execute them, and the range and saving of the history depend on shell settings.
Checking command history
Enter the following command in an interactive Bash terminal. history is a built-in Bash command that outputs the history list of the current shell.
history
The output shows each item's number and command. For example, a line showing 42 pwd means that item number 42 in the shell's history is pwd. The numbers and content vary depending on the account, session, and history settings, so do not use the example numbers directly. The command you enter is recorded, not the result of execution.
To view only recent items, specify a number. The following command displays the last 10 entries in the history.
history 10
This history 10 command itself may be included in the list. If the desired command does not appear, check the history feature, storage size, and exclusion settings.
Searching and Re-running History
In the default interactive input of Bash, pressing the up arrow key allows you to recall and edit previous entries. Press Ctrl+R and enter a search term to search the history backward. You can check the found command's content and then execute or modify it.
In interactive Bash with history expansion enabled, !! expands to the previous history entry. !n refers to the item with number n in the list. For example, the following command runs the item if number 42 is in the current list.
!42
Make sure the number matches the actual history first. History expansion usually executes immediately after input, so be especially careful with commands like file deletion or configuration changes. In non-interactive shells, history expansion is usually disabled by default, and behavior may differ in other shells.
To just see the expansion result of the previous entry without executing it, wrap it in single quotes and enter it as follows. The command displayed on the screen will not be executed.
history -p '!!'
If you set shopt -s histverify, the expanded command will be displayed on the editing line in an interactive Bash using Readline, allowing you to check them before execution. To revert the settings in the current shell, use shopt -u histverify. For more details, refer to Bash history expansion documentation.
Where and when history is saved
Bash manages history as a list in memory and reads history from the file specified by HISTFILE at startup. The typical default file is ~/.bash_history, but this may vary depending on the environment or POSIX mode. You can check the current settings with the following command.
printf '%s\n' "$HISTFILE"
When a shell with active history exits, it is saved to a file, but if the file is unwritable, HISTFILE is not set, or is empty, it will not be saved. Multiple terminals each have separate memory histories, so commands executed in one may not immediately appear in another. According to the histappend option, the file will be appended or overwritten upon exit, and the result varies depending on settings and simultaneous sessions.
history -a appends new entries that have not yet been added in the current session to the history file. On the other hand, history -w overwrites the history file with the current list, which may result in losing history from other sessions. If you use multiple terminals, check this difference before using it. The save behavior can be found in Bash history feature documentation.
Check history size and exclusion settings
HISTSIZE controls the number of history entries in memory, while HISTFILESIZE controls the maximum number of lines in the history file. Multi-line entries are exceptions to the line count in the file. The current values can be checked as follows.
printf 'HISTSIZE=%s\nHISTFILESIZE=%s\nHISTCONTROL=%s\nHISTIGNORE=%s\n' "$HISTSIZE" "$HISTFILESIZE" "$HISTCONTROL" "$HISTIGNORE"
HISTCONTROL and HISTIGNORE can determine which inputs to exclude from history. For example, if HISTCONTROL includes ignorespace, input starting with a space may be excluded from the history list. This is only a setting related to shell history and does not erase history left in other logs or programs.
Deleting history and sensitive information
history -d n deletes item number n from the current shell's history list. First, check the actual number with history. history -c clears the entire current list, so it should be used with caution. These two commands do not automatically erase already saved history files or lists from other shells.
Avoid entering passwords or tokens directly on the command line, as they may remain in history or elsewhere. If secret information has already been exposed, do not assume that simply deleting the history resolves the issue; instead, replace the credentials and check the scope of the related exposure.




