VMware ESXi and vSphere Cluster Management
Linux Command Line History
Learn how to view, search, repeat, clear, and configure Bash command history, including keyboard shortcuts, history expansion, persistence, and privacy.
Linux shells can remember commands entered at an interactive prompt. This record is called command history. History makes it easier to repeat long commands, recover earlier work, avoid retyping, and find a command used previously.
This lesson uses Bash as the primary example. Other shells may provide similar features with different commands or settings.
What command history is
An interactive shell usually maintains a history list for the current shell session. When you open another terminal or start another shell, that process may have its own in-memory list.
Bash can also save history to a persistent file, commonly ~/.bash_history. This allows commands from one session to become available in a later session. The exact behavior depends on shell configuration, when history is written, and how many entries are retained.
Displaying command history
In Bash, the history builtin lists commands known to the current shell:
history
Each entry normally has a history event number, followed by the command text:
15 pwd
16 ls -la
17 find . -type f -name "*.log"
18 history
The event number identifies that history entry and can be used with history expansion. The number of entries shown and retained depends on Bash settings and the current session.
To display only the most recent entries, provide a numeric count:
history 20
This requests the latest 20 entries. It does not necessarily mean that only 20 entries exist in memory or in the history file.
Repeating a command by history number
History expansion is shell syntax that refers to an earlier event. In Bash, an exclamation mark followed by an event number runs that entry:
history
!17
If event 17 is the desired command, Bash expands !17 to that command and executes it.
For safer reuse, display the history again immediately before using the number, or recall the command with the keyboard and review it at the prompt.
Recalling commands with the keyboard
At a Bash prompt, the Up Arrow key moves backward through previous commands. Pressing it once usually recalls the command entered immediately before the current prompt. Pressing it repeatedly moves farther back.
The Down Arrow key moves forward again through commands recalled with the Up Arrow. When you reach the newest entry, another Down Arrow generally returns to a blank prompt.
A recalled command is editable. Move the cursor, change an option or path, and press Enter only after reviewing the complete command.
| Task | Command or key binding | Result | Important caution |
|---|---|---|---|
| Display history | history | Lists numbered entries known to the current shell. | The list may differ between terminals. |
| Display recent entries | history 20 | Shows the most recent 20 entries. | The requested display count is not the retention limit. |
| Execute a numbered event | !17 | Expands and executes event 17. | Verify the exact entry before execution. |
| Previous command | Up Arrow | Recalls an older command. | Edit and review it before pressing Enter. |
| Next command | Down Arrow | Moves forward through recalled commands. | It does not execute a command by itself. |
| Reverse search | Ctrl+R | Searches backward through command text. | The first match may not be the intended one. |
| Find older reverse-search match | Ctrl+R again | Moves to an older matching command. | Continue reviewing the displayed command. |
| Clear current history | history -c | Removes the current shell's in-memory history list. | It does not automatically remove every persistent copy. |
Searching command history interactively
Bash commonly uses the readline library for command-line editing and history navigation. Readline provides reverse incremental search with Ctrl+R.
- Press
Ctrl+Rat the prompt. - Type part of the command you remember, such as
dd. - Review the matching command displayed by the shell.
- Press Enter to accept and run it, or edit the command before pressing Enter.
- Press
Ctrl+Rrepeatedly to move through older commands containing the same search text.
The search matches text in previously recorded commands. Use a shorter fragment if the search is too specific, or a more distinctive fragment if it returns too many results.
In typical Bash/readline environments, Enter accepts the displayed command and runs it. Arrow keys usually leave the search and place the command into the editable prompt. Esc or Ctrl+G can cancel the search without running the command.
Clearing command history
To clear the current shell's in-memory history list, run:
history -c
This is useful when commands accidentally contain secrets or other sensitive details. However, clearing the in-memory list is different from removing commands already written to the persistent history file.
If persistent removal is required, identify the configured history file and handle that file separately and carefully. A command may also still exist in another terminal's in-memory history, terminal scrollback, system or application logs, process records, backups, or shell history files copied elsewhere.
History privacy and safe command entry
Do not normally place passwords directly in command arguments. They may be saved in shell history, displayed in process listings while the command runs, captured by monitoring tools, or recorded in logs.
Prefer an interactive password prompt, a protected credential file with suitable permissions, an environment mechanism specifically designed for the tool and deployment, or a dedicated secret-management tool. The safest option depends on the program and operating environment.
Bash can be configured to omit selected commands. For example, when HISTCONTROL includes ignorespace, a command beginning with a leading space is commonly excluded from history:
sensitive-command --value "example"
This behavior is configuration-dependent and is not complete secret protection. A leading space does not prevent process listings, logs, terminal capture, or another shell from recording the value.
History persistence and Bash configuration
Bash uses several variables and options to control history:
| Setting or option | Purpose | Typical effect | Configuration note |
|---|---|---|---|
HISTFILE | Names the persistent history file. | Often points to ~/.bash_history. | A shell can use a different path or disable file persistence. |
HISTSIZE | Controls how many entries Bash retains in memory. | Older in-memory entries are discarded after the limit is reached. | This affects the current shell's history list. |
HISTFILESIZE | Controls the approximate number of lines retained in the history file. | The file is trimmed when Bash writes or manages persistent history. | File lines and history events are not always identical. |
HISTCONTROL | Controls omissions or duplicate handling. | Values such as ignorespace and ignoredups can exclude entries. | Its value is shell-specific and may be set in startup files. |
histappend | Controls how Bash writes history at shell exit. | Appends session history instead of replacing the history file. | Use shopt -s histappend to enable it in Bash. |
Inspect common Bash history settings with:
printf '%s\n' "$HISTFILE" "$HISTSIZE" "$HISTFILESIZE"
# Equivalent individual checks
echo "$HISTFILE"
echo "$HISTSIZE"; echo "$HISTFILESIZE"
To configure Bash to append history when a session exits, use:
shopt -s histappend
History is commonly written when a shell exits. With several terminals open, each shell can have a different in-memory view. Without append or synchronization settings, one shell can overwrite entries written by another, producing unexpected ordering or missing commands.
Advanced Bash configurations may use PROMPT_COMMAND to write or reload history around each prompt. Such synchronization can improve multi-terminal behavior, but it changes when entries are written and can make configuration harder to understand. Test customized settings before relying on them for auditing.
Troubleshooting missing or unexpected history
A command expected in history is missing
- It may have been entered in another shell or terminal.
- History may have been cleared.
- Retention limits may have removed older entries.
HISTCONTROLmay have excluded it.- The current shell may not yet have written its in-memory history to the persistent file.
Check the active shell and variables such as HISTFILE, HISTSIZE, HISTFILESIZE, and HISTCONTROL. Use history in the current terminal before relying on the persistent file. If several sessions are open, review whether append or synchronization is configured.
Ctrl+R does not find the desired command
- The search text may not match the recorded command text.
- A newer matching result may be displayed first.
- The command may not have been recorded or may have been trimmed.
Try a shorter or more distinctive fragment. Press Ctrl+R repeatedly to search older matches. You can also use history and other filtering tools to inspect entries that are currently available.
history -c did not remove a sensitive command permanently
The command may already be in the persistent history file, another terminal may still hold and later write its copy, or the value may exist in logs, scrollback, or process records. Distinguish current-session memory from persistent storage, address active shells and the configured history file carefully, and rotate any exposed credentials.
A history number runs the wrong command
Event numbers change as commands are added, and numbers from different sessions do not necessarily correspond. Run history again and verify the entry immediately before executing it. For safety-sensitive commands, use Up Arrow or Ctrl+R so you can review the complete command at the prompt.
Key terms
- Command history: A shell-maintained record of commands entered in an interactive session.
- History command: A shell builtin that displays and manages history entries.
- History event number: The numeric identifier beside a history entry.
- History expansion: Shell syntax, commonly beginning with
!, that refers to an earlier event. - Reverse incremental search: Interactive backward searching, commonly started with
Ctrl+R, as text is typed. - Readline: The command-line editing library used by Bash and other programs for key bindings and history navigation.
Practical checklist
- Run
historyto list available commands. - Use
history 20when you need only the latest entries. - Use Up Arrow or Ctrl+R to recall commands and edit them safely.
- Use event numbers only after verifying the exact entry.
- Keep passwords and tokens out of command arguments.
- Remember that
history -cclears the current in-memory list, not necessarily persistent or external copies. - Check Bash history settings when multiple terminals behave unexpectedly.
For a focused reference, see Linux command line history.