How to check if I am in a running screen session

Last updated on October 19, 2020 by Dan Nanni

Question: I often use screen command to run multiple pseudo terminal sessions. Oftentimes I find it difficult to remember whether I am inside a screen session, or in a regular terminal. Is there any convenient way to check whether or not I am inside a running screen session?

One quick and easy way to tell whether you are inside a screen's pseudo terminal or in a regular terminal is to check STY environment variable. When screen is invoked, it sets STY variable to a session name. So if STY environment variable is set to any non-empty string, that means you are inside a screen session.

$ echo $STY
2858.pts-3.my_host

However, note that the above method works only if you are still on a local computer where you run screen command. If you SSH to any other remote computer from the local computer's screen session, the STY variable will not be set on the remote computer while you are technically still in a screen session.

In fact, a more reliable way to check if you are in a screen session is to press Ctrl-a+Ctrl-t. If you are in screen, the terminal will show you time information at the bottom of the terminal.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean