[ This can be considered step 2 of my previous question Is it possible to change GNU screen session name after created? ]

Actually, I'd like to write a script that can display current screen session name and change current session name.

For example:

sren armcross

It will change the session name to armcross (ARM gcc cross compiler) and output something like:

screen session name changed from '25278.pts-15.linux-ic37' to 'armcross'

So, the key question now is how to get current session name. Not only for display the old session name, but according to Is it possible to change GNU screen session name after created? , I have to know it(pass to -d -r) before I can change it to something else.

Can we use $STY for current session name? No. $STY will not change after you have changed the session name to a user-defined one. However, for command

screen -d -r <oldsessname> -X sessionname armcross

<oldsessname> should be the user-defined name(if ever defined) instead of $STY, otherwise, screen spouts error "No screen session found."

Maybe, there is a verbose way, use screen -list to list all sessions(user-defined name listed), then, match the pid part from $STY against those listed sessions and we will find current session's user-defined name. It should not be so verbose for such a straightforward question. Don't you think so?

The -d -D and -r -R options seems to expose too much implementation detail to screen's user. It seems, to rename a session, you have to detach it, then do the rename, then reattach it. Right?

My env: opensuse 11.3, GNU screen 4.00.03 (FAU) 23-Oct-06

Thank you.

2 Answers 2


could you try this one;

screen -d -r $(ps -o ppid -p $$ --no-headers) -X sessionname newsessionname

  • +1 from me. Thank you. If there is suitable chance, I'll try it. But for now, I've turn to tmux.
    – Jimm Chen
    Apr 8, 2012 at 16:18

It appears that when you set the sessionname manually screen changes the name of the relevant file in /var/run/screen/S-$USER/ but doesn't update the value of $STY which would be ideal.

We can still use the original value of $STY to get the current session's name though as $STY contains the PID that relates to this sessionname.

PID=$(echo $STY | awk -F"." '{ print $1}')

You can then use the PID to get the sessionname from /var/run/screen/S-$USER

SESSIONNAME=$( ls /var/run/screen/S-$USER/$PID* | awk -F"." '{print $2}' )

Now you have the sessionname in SESSIONNAME you can do what you want with it.

  • Allow me some time to understand how your command works. Thanks.
    – Jimm Chen
    Apr 9, 2012 at 2:49

You must log in to answer this question.

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