I'm trying to build a command that launches screen, creates four sessions with different names, and run four different commands.

I know how to do this manually:

1. screen
2. ./command1 args
3. CTRL-A :sessionname Session 1
5. (GOTO 2)

Can I do this with a bash script or something? How would I do so?

4 Answers 4


screen -dmS "$SESSION_NAME" "$COMMAND" "$ARGUMENTS" will spawn a screen running $COMMAND in the background.

You can see active sessions with screen -ls and reattach with screen -r "$SESSION_NAME".

Dead sessions can be killed with screen -wipe.

  • It gets the job done!
    – hughes
    Commented Feb 5, 2012 at 1:23
  • 8
    This command creates a new screen session for each command. It cannot be used to create a single screen session containing one window for each command (as the manual steps in the question do). Thus, it does not answer the question, and the .screenrc answer below should have been accepted instead.
    – Tey'
    Commented Jan 27, 2019 at 0:45
  • 1
    is there any real advantage to multi-windows-one-session if all you do is want to cheaply keep something running in the background?
    – LawrenceC
    Commented Jan 27, 2019 at 2:57

To start multiple sessions automatically, set up a .screenrc file, a config file for screen. In it, you can create sessions, start programs, change the working dir etc. I use it to initialise my screen session.

Simple exampe for a .screenrc file:

# don't display the copyright page
startup_message off

# increase scrollback buffer size
defscrollback 10000

# create windows
screen -t TODO vim TODO.txt
chdir src
screen -t coding vim main.c
screen -t run 

The screen commands above each create one screen session. -t sets the session's title; the rest of the line is the command to run and its parameters.

Thus, the first and second screen line start a session and launch vim inside. The third one just starts a session and drops you at a prompt. chdir changes the working directory for all subsequent sessions.

If you want to have multiple .screenrc files, just name them any way you want, and select one with screen -c myscreenrc.

  • 1
    Thanks to your answer, I was able to come up with a setup I wanted. I noticed that ~ doesn't work in the rc file, however $HOME does. Another thing I noted is that the screen command only supports simple (literal) arguments. Naturally, it's not a shell with all the fancy expansions etc. If these are needed, can just use, as an example, screen -t "http-server" bash -c 'http-server --hostname $(hostname -s)'. Commented Nov 15, 2022 at 15:20

You can use the d, m, S options together:

screen -Sdm s1
screen -Sdm s2
screen -Sdm s3

S : To create a screen

d : detach from a screen

m : To enforce creation of screen, regardless whether screen is called from within another screen or not.

  • 3
    Interesting, that you can write like this, instead of screen -dmS s1 etc. I didn't know that, when grouping options, the one with an argument needn't be last. Commented Aug 2, 2016 at 9:31

I have many cases where I need to open up a set of hosts for investigation or support. Because I only need these additional windows occasionally I decided to use a shell script rather than .screenrc approach.

for example in ~/bin/prod_support:


if [[ ! -z "$STY" ]]; then
        screen -t "hosta" ssh hosta
        screen -t "hostb" ssh hostb
        echo "start up screen first"

If run from a non-screen shell I get a reminder... if run from inside screen, it will add titled windows for each script run.

You must log in to answer this question.

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