1

Is there a way to list the windows of a screen session from a non-interactive terminal?

screen -S 'screen_name' -X windows

According to gnu.org, this command displays the list of windows using the message line.

That's fine and dandy for an interactive console, but I need this to print to standard output so I can read it into another program.

Any way to do this?

SOLUTION

I found that the best way to do this is to get the list of windows ids by using this command: (as posted by Stephen Harris)

screen -S 'pid.screen_name' -Q windows

Although this truncates the names of the windows when it prints, it doesn't actually truncate the total size of the output. So if I have like 20 windows in a screen session, this is what the output will be.

> screen -S 'pid.screen_name' -Q windows
0 bash  1 bash  2 bash  3 bash  4 bash  5 bash  6 bash  7 bash  8 bash  9 bash  10 bash  11 bash  12 bash  13 bash  14 bash  15 bash  16 bash  17 bash  18 bash  19 bash  20 bash

I have tried this up to an absurd number of windows.

With this you can parse how many windows there are in the screen session, and then issue a "title" command for each one using the following command:

screen -S 'pid.screen_name' -p %window_id% -Q title

Replace %window_id% with each window ID from the windows command.

This time the window title isn't truncated at all. So the full screen name is outputted to standard output.

1

You can use the -Q option

% screen -S 2908.pts-0.mymachine -Q windows
0- spam  1* news  6 ksh

Unfortunately this truncates if there's more than fits across the terminal width (basically it causes the command to run and displayed in the message line, but also copies it to the current stdout).

  • It seems like it cuts off at much smaller than the terminal width. For me it's like 22 characters. Is there some way to increase the terminal width temporarily, execute the command, and then restore it? – Bradley Odell Jul 18 '16 at 21:44
  • It actually trims the window title to 20 characters. – Bradley Odell Jul 18 '16 at 21:55

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.