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)


1 Answer 1


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
  • 1
    "$@" ⁠⁠⁠⁠⁠⁠⁠⁠
    – Roger Pate
    Oct 29, 2010 at 7:47

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.