Attach to 'screen' session with creating a new screen window

Alfe picture Alfe · May 15, 2012 · Viewed 13.3k times · Source

I have a screen session running with several windows. I want to attach to it, create a new screen window within it and start a shell in that new window.

Question: How can I do this from the command line outside the screen session?

I already tried a lot, e. g. screen -x (but it attaches to one of the existing screen windows; it does not create a new one nor does it start a new shell). Any hints are welcome.

Environment is Linux (Ubuntu 12.04).

Answer

Alfe picture Alfe · May 15, 2012

I found something on the mailing list, thank you tuxuday :)

I'm doing it now this way:

#!/bin/bash
screen -X screen -t NEWWINDOW  # create new window (switches existing attached terminal)
sleep 0.1
screen -X other  # switch existing attached terminal back to its old window
sleep 0.1
gnome-terminal -e 'screen -x -p NEWWINDOW'  # connect to new window

I'm not sure about those sleeps, maybe they aren't necessary in all environments, but I can wait those 0.2s easily.

My .bash_aliases is changing the screen window title with a delay, so the awkward NEWWINDOW won't stick for long (and thus not hinder further calls to this script).