I use different screen sessions for different projects. Starting screen like screen -S project1. Now, I'd like to mention 'project1' in hardstatus line.

Session name can be obtained from environment variable STY: STY=13539.project1.
But how to get this into screen? I've tried backtick command and %` in hardstatus, but I can't seem to get it right.

What I did:


hardstatus string '%H:%`'
backtick 0 30 30 echo $STY

no luck, empty %`.

backtick 0 30 30 sessionname

still no luck, sessionname: Not found

2 Answers 2


You can include this string (with additional information, if desired) in your $PS1:


inside single quotes in order to delay evaluation of the variable. Then add this to your ~/.screenrc:

hardstatus string '%H:%h'

Unfortunately, screen doesn't set $STY in the environment of the commands it spawns for the backtick feature.

Another option, instead of the one above:

hardstatus string '%H:%`'
backtick 0 30 30 sh -c 'screen -ls | grep --color=no -o "$PPID[^[:space:]]*"'

The advantage of this one is that it follows changes made by using the sessionname command. The first option doesn't.


From here:

Since $STY is not set yet when screen sources .screenrc, you can use this trick in your .screenrc:

    screen $SHELL -c 'screen -X caption always "$STY"' 

I.e. send a screen command to the first window.

  • 1
    For the solution that starts with backtick: (1.) is backtick a bash builtin? a gnu-screen command? a binary executable? is there a man page? (2.) I know it works because I tried it but how do you get a two line command to execute when it seems like the gnu-screen .screenrc syntax requires one line for the hardstatus command? (3.) how would I modify your command to allow appending some text after the session name? Nov 12, 2015 at 21:17
  • @TrevorBoydSmith: backtick is a screen command. Search for it in the man page for screen. It might be best if you post a new question regarding the rest of what you're asking. I don't think hardstatus output can occupy two lines on-screen. You could use sed or awk instead of grep to do what grep is doing in my example and also append some extra text. Nov 12, 2015 at 22:27
  • When I said "two line" let me restate what I meant, I don't want a two line display for hardstatus. I was confused by the %` and the backtick being on separate lines... they didn't look related but now that I read the documentation for backtick I see that the two are related/necessary/valid-syntax (albeit rather strange syntax IMO). Nov 13, 2015 at 14:33

For me this easily works with inserting %S in the hardstatus.

MWE (.screenrc):

hardstatus on
hardstatus alwayslastline
hardstatus string "%S"

However, this displays the session name without the ID (like ${STY#*.}); in your example: project1.

(Same answer to other questions here and here for completeness).

  • 2
    FYI, The "%S" does work on newer versions of gnu-screen (works on Fedora 21 version for me) but not older versions (does not work on CentOS 6 version for me). Nov 12, 2015 at 21:14

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.