How can I send commands to a GNU Screen session? If I was using the session interactively, I'd type a Ctrl+A escape sequence.

I need to send specific keystrokes to a GNU SCREEN session from other locations (could be a BASH Shell Script, a PHP page, whatever) to manipulate a program that only works as a "text-based UI" (like lynx or vim). From time to time, I need to see what's going on in the SCREEN, so I export the view using the hardcopy -h outputFile command.

I need to "stuff" Ctrl+A then : into a screen, but it won't let me send Ctrl+A. I tried this:

$ screen -S test -p 0 -X stuff '^A:'

But that only puts a colon (:) in the screen and ignores the ^A.

What I currently do (not working, need manual intervention to complete 3rd step):

  1. Setup the Screen

    $ screen -S test

  2. Inject commands to Screen

    $ screen -S test -p 0 -X stuff 'python^M'
    $ screen -S test -p 0 -X stuff '42*9^M'

  3. From within the screen, you can do Ctrl+A then : to bring up the Screen's COMMAND input. In that input, I type the following:

    : hardcopy -h /tmp/scr-test.txt

  4. I can then fetch the contents of the screen by cat-ing the file:

    $ cat /tmp/scr-test.txt

  5. Which produces:

    $ cat /tmp/scr-test.txt
    justin@justin2012:~$ python
    Python 2.7.8 (default, Oct 18 2014, 12:50:18)
    [GCC 4.9.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 42*9

  • What about using eval? As in screen -S test -p 0 -X eval "hardcopy -h sample"
    – Runium
    Dec 2, 2014 at 19:25
  • or even just screen -S test -p0 -X hardcopy out.txt Dec 3, 2014 at 17:59

2 Answers 2


screen -S test -p 0 -X stuff '^A:' does work, but it does what it's supposed to do, not what you're trying to do: it sends the two characters ^A and : to the application running in the screen window. That's what the stuff command does: it sends characters to the application running in the screen window, not to the screen interface.

What you want to do is send a command to screen itself, not to the application running inside screen. So don't use the stuff command: that's not what it's for. The feature you're looking for is simply screen -X …, of which screen -X stuff … is a special case.

For example, to make screen execute a hardcopy command, run

screen -S test -p 0 -X hardcopy -h outputFile 

To achieve what I wanted, users Sukminder and lemonsqueeze provided 2 variances of a way to access the Ctrl+A+: interface, which is great.

Sukminder wrote:

What about using eval? As in screen -S test -p 0 -X eval "hardcopy -h sample"

lemonsqueeze wrote:

or even just screen -S test -p0 -X hardcopy out.txt

I've tested and both solutions work. I think I'll still use Sukminder's solution, since it allows me to keep the -h parameter.

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.