Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a screen instance running, and I would need to execute some code inside the screen, and get the result out to my script.

The first part is quite easy, I just screen -S session_name -X eval 'stuff "$cmd"\015'.
(I modified a line I found in a script)

The second part, getting out the output, is trickier. How can I get the whole output, whatever it's size?

share|improve this question
Thanks :) The second link was explained why the first command has to be stuff, which was what I had trouble understanding. – 1ace May 5 '11 at 13:44
up vote 17 down vote accepted

You could start screen with the -L option. This will cause screen to create a file screenlog.n (the n part is numerical, starting with a zero) in the current working directory.

In your case this would look something like: screen -S session_name -L -X eval 'stuff "$cmd"\015'

As long as you remember to clean up afterwards, this should match what you are after.

For last line of the log, it can easily be obtained with tail -1 screenlog.0, or the entire log can be parsed however you wish.

share|improve this answer
One thing, though: the screen command I gave was just to send the command in. To start the screen, I used screen -dmS name program ;-) – 1ace May 4 '11 at 15:02

One more nuance that may be helpful in the future: If you're in a screen session, you can interactively ask for the log file to be created. Use the C-A H (Control-A then capital H) keystroke to ask screen to start dumping whatever window you're in to a log file.

share|improve this answer
Don't know if it is because of a newer version but my man screen says, I have to type C-A H, without the Control for the H. Sad only, that this doesn't capture the previous log. – Matmarbon Aug 1 '12 at 10:43
You're right! It's C-A H to generate a log file. Please note that it's capital H (lowercase h produces just a hardcopy of the current screen). – rickumali Aug 4 '12 at 11:52
Please update the answer to C-A H. Future searchers may be mislead and miss the correct answer. – fbrundu Feb 22 '14 at 0:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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