I use GNU screen with 5 different sessions, each of which stores its own command history. But often I don't find a command in the history, although I'm sure I've used it exactly in the session I'm currently in, a few weeks ago.

It seems there is a hard limit on the history size? How can I set that to infinity?

A related question: How can I control where the history of a session is stored? Often, our sysadmin reboots the computer(s) and I lose all my sessions, and I have to do screen -wipe because the sessions are corrupted. Then, all histories are gone. :( I'd like to find the stored histories in that case and bind them in a newly created session.

P.S.: I use bash in all screen sessions.

UPDATE: I am not asking about how to unify the session histories. Just, I want each session to have an infinite history, and I want to be able to 'load' that history into a new session, should the old one get corrupted or deleted.

UPDATE 2: You've probably figured out: When I said I use GNU screen with 5 different sessions I really mean a GNU screen session with 5 different windows in it. Sorry.

4 Answers 4


You'd be better off using Bash's history than screen's. Screen keeps a scrollback buffer (probably in-memory rather than in a file). When you recall commands using Ctrl-a { it's actually digging through everything that appeared on the screen that's still in the buffer that looks like it follows a prompt character. There's not really a command history. You can increase the size of the scrollback buffer using screen -h num or the defscrollback num or scrollback num screen commands, by the way.

You can use warren's suggestion to keep your Bash history up to date. And/or you can use one of my logging functions found here that can save your IP address or screen session ID along with date, time, current working directory and actual command. I use this all the time myself. You may have to set the variable $hcmntextra, which is used by my functions, to include $STY so the screen session name is logged, too.

  • Thanks. One thing, when I talk about the different sessions keeping different histories I mean: when I type the history command in different sessions I get the commands that I typed in those sessions. I'm not talking about the scrollback buffer. Or is that the same?
    – user9474
    Dec 6, 2009 at 21:50
  • Yes, screen's history command is bound to Ctrl-a { by default. If you press that or use that command, screen does a screen-scrape looking for recent commands. If you type a character or two then do that it tries to match what you typed to the beginning of a previous command. (If after this, you press up-arrow, for example, you're then using Bash's history - not screen's.) For an example of how screen history doesn't work (for me): If I call up a man page and exit, then type a letter that appears in the man page but not in a recent command, then do a Ctrl-a { - I get text from the man page. Dec 7, 2009 at 0:39

I asked a similar question on SU a bit ago: https://superuser.com/questions/37576/can-history-files-be-unified-in-

To quote the accepted answer:

here are two things you need to do:

Insert the command shopt -s histappend in your .bashrc. This will append to the history file instead of overwriting it.
Also in your .bashrc, insert PROMPT_COMMAND="$PROMPT_COMMAND;history -a; history -n" and the history file will be re-written and re-read each time bash shows the prompt.

See also the history man pages.

  • Actually that is not what I meant. I like the fact that the histories of different sessions are kept apart. I do relatively unrelated stuff in the different sessions. I just don't want the histories to be purged automatically. I will update the question to make this clearer.
    – user9474
    Dec 6, 2009 at 18:51
  • ok - I thought you were looking to join them
    – warren
    Dec 6, 2009 at 19:52
  • 1
    Did just the trick that I needed. Hard to picture why shopt -s is not a default! Mar 28, 2016 at 18:18

My preferred solution to this problem is a variation of one of the previous answers.

I like to have a separate file stored for each screen session/window combination and each file is appended each time its corresponding prompt is shown:

if [[ "$STY" = "" ]]; then STY="none"; fi
if [[ "$WINDOW" = "" ]]; then WINDOW="none"; fi
export HISTFILE=~/.bash_history.$STY.$WINDOW;


This creates the following history files:


If you want window {x} to always use its own history file but use the same file regardless of the screen session, you can merely leave out the STY variables:

if [[ "$WINDOW" = "" ]]; then WINDOW="none"; fi
export HISTFILE=~/.bash_history.$WINDOW;


This would create the following history files:


The command histories for each window are in memory for each bash session. You'd have to have bash write to different history files for them to persist and remain separate like you're asking.

You can do this by setting HISTFILE per screen session. I imagine the way to do this would be something like this in your startup file:

if [[ "$WINDOW" != "" ]] ; then export HISTFILE=~/.bash_history.$WINDOW ; fi

Note that this doesn't solve the problem of updating the history file when your shells are terminated by a reboot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.