Why does the up arrow bring back a command in one terminal when you can’t find it in another? Every interactive Bash shell keeps its own history list in memory. history shows the current list. When Bash starts, it reads old entries from the file specified by HISTFILE, which defaults to ~/.bash_history. On a normal exit it writes history back to the file according to its settings. Two terminals open at the same time may therefore show different records.1
How do I keep commands long term and find them again?
“Unlimited Bash history” discusses two limits that are easy to confuse. HISTSIZE controls how many commands the current shell keeps in memory, and HISTFILESIZE controls how many lines the history file retains. If you increase only the first, the file can still be truncated on exit. In Bash 4.3 and later, you can remove both limits by adding the following lines after the existing history settings in ~/.bashrc.23
HISTSIZE=-1
HISTFILESIZE=-1
shopt -s histappend
HISTTIMEFORMAT='%F %T '
After you reopen Bash, histappend makes each terminal append its new entries on exit, reducing the risk of terminals overwriting one another. HISTTIMEFORMAT tells Bash to save timestamps. Run history 20 to see the last 20 entries with their dates and times. To find the arguments used in a particular build, press Ctrl+R and type build, then press Ctrl+R again to search further back. Setting the display format later cannot recover timestamps that were never saved.1
With both terminals open, run history -a in terminal A to append the new entries from that session to the file. In terminal B, run history -n to read the newly added entries. You can now recall A’s recent command in B without closing A.4
What if I accidentally typed my password at the Bash prompt?
When you enter a password at a sudo password prompt, sudo reads it. Bash history normally contains only the sudo command. The easy mistake is thinking the login box still has focus when you actually typed the password at a Bash prompt. If you haven’t pressed Enter yet, press Ctrl+C to cancel the line. If you have pressed Enter, even a simple command not found message can mean the password has already entered history as a command.
Go back to that terminal, check that you’re not sharing your screen, and run history 5 to inspect the last few entries. If the number beside the accidental entry is 418, run history -d 418, substituting the number you actually see. This removes just that entry from the current shell’s history list without making you type the password again in a search command. The original discussion also mentions unset HISTFILE, which makes the current Bash shell skip writing the whole session’s history when it exits.56
If the terminal automatically runs history -a every time it displays a prompt, the password may already be in the history file by the time you see the error. Neither operation above undoes a write to disk. If the password was written to disk, appeared in a screen share or a session recording, or you’re unsure who could see it, change it at the relevant service first. Then you can remove the accidental line from your own history file, normally ~/.bash_history.14
Does history tell me whether a command succeeded?
Suppose your history contains cp "$src" "$dst". It lets you recover what you typed. The Bash manual says that commands are recorded “prior to parameter and variable expansion”, so this entry retains the variable names. On its own, it can’t tell you the two paths at the time, the exit status, or whether the file was copied. Conversely, a missing command might simply mean its terminal hasn’t written history yet.1
When retracing your own steps, use history to find commands and approximate times, then check build logs or file changes for the outcome. To independently establish who performed an operation and when, you also need audit logging enabled in advance and stored separately by the system.
References
Footnotes
-
Free Software Foundation. (n.d.). Bash history facilities. GNU Bash Reference Manual. §9.1, first paragraph on recording before expansion, followed by history file reads, writes, and timestamps. ↩ ↩2 ↩3 ↩4
-
Lri. (2012, September 2). Unlimited Bash history [Answer, including a version note for Bash 4.3]. Stack Overflow. Answer about the two history limits. ↩
-
Free Software Foundation. (n.d.). Bash variables. GNU Bash Reference Manual. §5.2, entries for HISTFILESIZE and HISTSIZE. ↩
-
Free Software Foundation. (n.d.). Bash history builtins. GNU Bash Reference Manual. §9.2, the -a and -n history options. ↩ ↩2
-
Bronosky, B. (2018, September 6). Don't save current bash session to history [Answer]. Stack Overflow. Section on deleting a password or command from history. ↩
-
user123444555621. (2012, January 27). Don't save current bash session to history [Answer]. Stack Overflow. Answer about
unset HISTFILE. ↩