How to check if I am in a running screen session

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

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, 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.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

Leave a comment

Your email address will not be published. Required fields are marked *