In Ubuntu 24 how to retrieve `screen` specific Bash history?
May 13, 9:32am

In Ubuntu 24.04 (or any Linux system using screen), the Bash history is typically stored in ~/.bash_history by default. However, if you're using the screen terminal multiplexer, each screen session can have its own scrollback buffer, but Bash history itself is not stored per-screen session by default.

How to Retrieve Bash History for a Specific screen Session

1. Check if HISTFILE is Set in the Screen Session

  • If you or your system has configured screen to use a custom HISTFILE (e.g., per-session history), you can check the value of HISTFILE inside the screen session:
    bash
    echo $HISTFILE
  • If it points to a custom file (e.g., ~/.bash_history_screen123), that file contains the history for that session.

2. Default Behavior: Shared Bash History

  • By default, Bash history is shared across all terminal sessions (including screen sessions) and stored in ~/.bash_history.
  • To view the history of the current screen session, simply run:
    bash
    history
    This shows the history of the current Bash session, which may include commands from other sessions if HISTFILE is shared.

3. Enable Per-Session Bash History in screen

  • If you want each screen session to have its own history, you can configure it by:
    1. Setting a unique HISTFILE for each screen session: Add this to your ~/.bashrc:
      bash
      if [ -n "$STY" ]; then
          HISTFILE=~/.bash_history_$STY
      fi
      • $STY is an environment variable set by screen that contains the session ID (e.g., 12345.pts-0.server).
      • This will create a separate history file for each screen session.
    2. Reload ~/.bashrc: After editing, reload it in all screen sessions:
      bash
      source ~/.bashrc

4. Retrieve History from a Specific screen Session

  • If you've set up per-session history (as above), the history for a specific screen session will be in:
    text
    ~/.bash_history_<session_id>
  • Example:
    bash
    cat ~/.bash_history_12345.pts-0.server

5. Alternative: Use screen's Scrollback Buffer

  • screen itself does not store command history, but it does store scrollback (output). To save the scrollback of a screen session to a file:
    1. Enter the screen session.
    2. Press Ctrl+A then : (colon) to enter the command mode.
    3. Type:
      text
      hardcopy -h /path/to/savefile.txt
    • This saves the visible scrollback to the specified file.

Summary Table

Bash History in screen
Default Bash History
~/.bash_history
Shared across all sessions
Per-Session History
~/.bash_history_$STY
Requires ~/.bashrc configuration
View Current Session History
history
Shows current session's history
Save Scrollback
hardcopy -h /path/to/file.txt in screen
Saves visible output, not command history

Key Takeaway

By default, Bash history is shared and stored in ~/.bash_history. To have per-screen session history, you must configure HISTFILE dynamically in ~/.bashrc. If you just want the output (not commands), use screen's hardcopy feature.

May 13, 9:32am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Le Chat can make mistakes. Check answers. Learn more