How to get the output inside `screen` out to a script?

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.


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. Press ctrl-a H (Control-A followed by capital H) to ask screen to start dumping whatever window you're in to a log file.