My goal is to write a zsh script in order to create multiple screen sessions and run separate commands on each of them. I would also like to check at the beginning of my script if there are any screens with running jobs and terminate all of them that sit idle. Furthermore, if possible I would like to have a way to record the stdout of the individual screen and get their ID number. So far I have tried the following:

### Script for running everything in screens ###
killall -15 screen ## We make sure that no screens are running for now
#bkg_array = ("TopJets" "BosonJets" "DiBoson" "TTbar")

screen -dmS "MYSCREEN"
screen -S "MYSCREEN" -p 0 -X stuff 'echo "The array has of elements."\n'

However, I cannot seem to send the keystroke for pressing Enter; how can I achieve that effect? Additionally without the -p option the command is not sent to the screen. After pressing Ctrl+V+Enter the characters ^M were pasted on the screen and it pressed enter in the screen session, but I have no idea why that worked.

Thanks for your help, feel free to point out any ambiguities.

  • 3
    instead of making problem, use tmux Aug 21, 2014 at 14:39
  • @MohsenPahlevanzadeh Thanks, but tmux is not installed on the machine I am running the shell script on and I have no administrator privileges on the computer.
    – Vesnog
    Aug 21, 2014 at 14:51
  • 2
    Forget tmuxand forget screen. Just use zpty. Youll automatically get that array you want. Combine that with ztcp and youve got remote access.
    – mikeserv
    Aug 21, 2014 at 16:43
  • Do man zsh-modules for information on both. Else, look here for a simple-minded zpty demo.
    – mikeserv
    Aug 21, 2014 at 17:16
  • 1
    You're sending an LF character, which bash and zsh as well as the tty line editor (with default bindings) treat like CR which is the character sent by the Enter key. What is received inside the screen window (attach the screen to look)? Aug 21, 2014 at 22:46

2 Answers 2


I find that while screen doesn't interpret named characters such as \n, it does take care of octal escapes. So instead of stuff 'echo "The array has of elements."\n', you could use:

stuff 'echo "The array has of elements."'\012

So what I do is this:

screen -dmS SCREEN_SESSION_NAME -t TAB_NAME ~/bin/my_first_prog.sh ; screen -S SCREEN_SESSION_NAME -X screen -t TAB_NAME2 ~/bin/my_second_prog.sh

At the the you will have a screen session, called SCREEN_SESSION_NAME, with two tabs. To exit from everything I kill the programs gracefully ( kill -1 ) and the run


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.