I've written a script (that doesn't work) that looks something like this:


screen -dmS "somename" somecommand

for i in {0..5}; do
    screen -dmS "name$i" anothercommand $i

For some reason, if I copy and paste this into a terminal, it creates 7 detached screen sessions as I expect. If I run it from within a script, however, I get only the first session, "somename," when I run screen -ls.

Edit: If the same can be accomplished another way (e.g. with multiple screen windows instead of sessions), I would be open those solutions as well. Thanks!

4 Answers 4


I ended up taking this question to StackOverflow, where Brian Gerard answered the question. The {0..5} loop syntax is bash (3.x+) specific. By default, my system was setup to run some other shell from /bin/sh, so I changed my sharp-bang to #!/bin/bash and my problem was solved!

  • 2
    For the reference, this is the answer on SO.
    – koniiiik
    May 9, 2013 at 13:36

Why do you need to open so many screen sessions? Instead, why don't you try creating multiple windows (I mean as a part of single screen terminal window) inside a single screen session. You can switch between them with ctrl-a 1 or 2 or 3 etc, depending upon how many you have created and want to view the output.

This sounds like a bad approach to running screen. In case you have not heard of multiple windows in screen, read up any tutorial on screen on the web. Screen's main design goal is window multiplexing - not just attaching and detaching.

  • 1
    Exactly, screen is really designed to have one master session managing multiple terminal sessions. No need to spawn multiple master screens.
    – Keith
    Jul 11, 2011 at 2:06
  • 3
    Ok, good advice. I would certainly like to take advantage of the windows within a single screen session. Any idea how I would do this in an automated fashion from my script (to accomplish the same as the above script)?
    – Eric
    Jul 11, 2011 at 15:17

Unfortunately, I can't answer Your comment because I have too little reputation, so I'll have to put it into a seperate answer.

You can create a single screen session with multiple windows using a screenrc configuration file. The screen manpage tells You everything, but here's the most important things that should solve Your problem:

The following screenrc creates a screen session with 2 windows. One window will be running bash, the other will be running python (interactively):

sessionname myscreensession
screen -t command1 0 bash
screen -t command2 1 python

The name myscreensession shows when executing screen -ls and can be used as a parameter for screen -r. The strings commandN specify the window names within the screen session. The numbers (0 and 1) specifiy which window to run the command in (You don't have to use subsequent numbers).

You can also later add another window with a new command to the running screen session, e.g. with:

screen -X screen -S myscreensession -t command3 2 python3

This would create a new window running python3 in the existing session.


I don't really know why your script does not work but another way to start multiple screen sessions is to create a custom screen config file. It was discussed in this AskUbuntu question: https://askubuntu.com/questions/49245/start-multiple-apps-running-in-shells-split-in-one-console

I guess you could simply create the config file test.conf with the following content and launch screen -d -c test.conf

screen -S "somename" somecommand
screen -S "name1" anothercommand 1
screen -S "name2" anothercommand 2

And if it works, then your script could first create this test.conf file and customize it in the loop with

echo "screen -S "name$i" anothercommand $i" >> test.conf

and then start screen -d -c test.conf at the end of script.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .