On Ubuntu 24.04 (and most Linux systems), the history command in Bash reads from the ~/.bash_history file, but only when the session starts. If you use screen (or tmux), each new shell inside screen starts a new session, which means it loads the history from ~/.bash_history at that moment—but it does not automatically sync changes back to the file when the session ends.
history different before and after screen?screen: Your main terminal session has its own in-memory history, which is written to ~/.bash_history only when the session exits cleanly.screen: Each shell inside screen has its own in-memory history. When you exit screen, the history from those shells is not appended to ~/.bash_history unless explicitly configured.Add these lines to your ~/.bashrc to ensure history is appended immediately after each command:
bashshopt -s histappend PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
histappend: Appends to ~/.bash_history instead of overwriting.history -a: Appends the current session's history to ~/.bash_history.history -c: Clears the in-memory history (to avoid duplicates).history -r: Reloads the history from ~/.bash_history.Also in ~/.bashrc, increase the history size to avoid losing commands:
bashHISTSIZE=10000 HISTFILESIZE=20000
If you have multiple terminals or screen sessions open, run:
bashhistory -a; history -r
This forces all sessions to sync with ~/.bash_history.
screen SpecificallyIf you want screen to share history with your main shell, ensure that:
screen) use the same ~/.bash_history.PROMPT_COMMAND trick above is applied to all shells.tmuxIf you switch to tmux, it handles history sharing more gracefully by default, as all panes/windows share the same environment.
screen and run a few more commands.screen and check history in your main terminal—it should now include commands from both sessions.Le Chat can make mistakes. Check answers. Learn more