Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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

Is it possible for GNU Screen to detect when a session has been attached and to run a command? (The command will be a shell script that I wrote)


share|improve this question
up vote 2 down vote accepted

What exactly are you trying to do? These probably don't exactly answer your question but you might be able to adapt them to your needs:

You could create a script called screenr that will reattach a screen session after first executing your script in that session.

screen -X exec /command/to/run
screen -r $@

Another idea might be to put something in your .bashrc to check if you're running screen and then execute a command. That will run whenever you start a new screen session or create a new shell in screen but it won't run if you attach an already running screen session.

if [ $TERM = "screen-256color-bce" ]; then

Edit: After seeing your other question it looks like the screen command setenv might be what you want, but it only sets environment variables for new shells started in screen, not already running ones.

screen -X setenv DISPLAY $DISPLAY
share|improve this answer
"$@" ⁠⁠⁠⁠⁠⁠⁠⁠ – Roger Pate Oct 29 '10 at 7:47

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.