Question: I want to write a bash script to re-attach to the existing linux screen, perform some commands and then detach from that screen. I know to create a new linux screen in detached mode and perform some commands. But I haven't found a way to re-attach to a existing linux screen.

Answer: 
try this; 

```
user@host:/screen -ls 

There are screens on:
        29229558.pts-5.host   (Detached)
        46661728.pts-22.host   (Detached)

user@host:/screen -r 46661728.pts-22.host

```

If you see (attached) as below; 

```
user@host:/screen -ls
There are screens on:
        29229558.pts-5.host   (Detached)
        46661728.pts-22.host   (Atached)

screen -D -r 46661728.pts-22.host

```
To re-attach to the screen you have to follow this steps:

Before de-attaching from the screen, execute:

```
STDOUTTERM=$(who -m|awk '{print $2}')

```

Then you can attach stdout to whatever you want. When you finish, to reattach to stdout, execute this:

```
exec > /dev/$STDOUTTERM

```