Home GNU Screen How To Check If You Are In Screen Session Or Not In Linux

How To Check If You Are In Screen Session Or Not In Linux

By sk
Published: Updated: 19.2K views

This brief guide explains how to check if you are in Screen session or not in Linux. As you might have noticed, when you launch a Tmux session, you will obviously see the session details, such as the current SHELL name, hostname and current date and time, in the status bar at the bottom as shown in the below picture.

Tmux session details

Tmux session details

But you will not see such details when you are inside the Screen sessions. Here is how the screen session looks like on my Ubuntu system:

Screen session

Screen session

Can you spot the difference? The screen session and normal session will exactly look like same. Let us run "screen -ls" command to list of the running screen sessions.

$ screen -ls

Sample output:

List screen sessions

List screen sessions

See? I am inside the Screen session, but it looks like normal session. There is no indication in the status bar at the bottom. How do you know whether you are working inside a Screen session or not? That's what we are going to find out now.

Check If You Are In Screen Session Or Not In Linux

There are couple ways to verify if we are in Screen session. Here I have given three methods.

Method 1 - Using "echo" command

To check whether you are in Screen session or not, simply run the following command:

$ echo $STY

If you're inside Screen session, you will see an output like below.

18704.pts-0.ostechnix

Here is another way to check if you're in Screen session:

$ echo $TERM

You will see term "screen" in the output.

Sample output:

screen.xterm-256color
Check If You Are In Screen Session In Linux

Check If You Are In Screen Session In Linux

If there were no running screen sessions, you will see nothing for the first command. And for the second command, there won't be the term "screen" in the output.  Just exit or detach from the screen session and run the above commands again. You will see an output like below.

Check screen sessions

Check screen sessions

See? We are not inside the Screen session.

The above two commands will work only if you are in the running screen session on your local system. It will not work if you started the Screen session in the local system and then access some other system on the network via SSH.

In that case, you can use the second method given below.

Method 2 - Using Screen prefix key

To check whether we are in Screen session or not, simply press Ctrl+a and then Ctrl+t keys. This will show the time and host name if you are in Screen session.

Have a look at the following demo.

Check if you are in Screen session using Screen prefix key

Check if you are in Screen session using Screen prefix key

As you in the above output,

  • First, I started a Screen session and press Ctrl+a and Ctrl+t keys to check if I am in Screen session.
  • Then I detached from the Screen session by pressing Ctrl+a and d.
  • And finally I reattached to Screen session and hit Ctrl+a and Ctrl+t to check again if I am in Screen session.

This method will work if you are even if you have SSH'ed to somewhere else.

Method 3 - Using Caption command

If you don't like to press the keys every time, you can add the following code in your ~/.screenrc file. If this file is not available, just create it.

$ nano ~/.screenrc

Add the code:

caption always "%{= kc}Screen session on %H (system load: %l)%-28=%{= .m}%D %d.%m.%Y %0c"

Save and close the file.

From now on, whenever you launch a Screen session, you will see a small notification bar (with including hostname, time and date, System load etc.,) at the bottom of the Terminal window as shown below.

Screen session on ubuntuserver (system load: 0.00 0.00 0.00)        Sat 22.02.2020 12:37
Display Screen session details at the bottom of Terminal window Using Caption command

Display Screen session details at the bottom of Terminal window Using Caption command

This notification bar will go away after you exit from the Screen session.

Method 4 - Set different Bash prompt for Screen sessions

This is yet another way to make Screen sessions easily recognizable by adding a few lines of code in ~/.bashrc file. Actually this is not recommended method. It may cause many unnecessary problems if another shells like zsh inherits your current shell. This is just for educational purpose only. Use it at your own risk!

First, backup your current ~/.bashrc file:

$ cp ~/.bashrc ~/.bashrc.bak

Just set different Bash prompt name for normal and Screen sessions as you wish like below. For example I have added the following lines in my ~/.bashrc file.

if [ -z $STY ]
then
        PS1="This is normal session:~$ "
else
        PS1="This is Screen session:~$ "
fi

Log out and log back in. Your Terminal prompt name will change as per your settings while you're switching between normal and screen sessions.

Set different Bash prompt for Screen sessions

Set different Bash prompt for Screen sessions

Again, It is not a good practice to change Bash prompt name. Instead, just use any one of the previous methods to check if you're in Screen session or not.


Related read:


Hope this helps.

Resource:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

2 comments

poolpog January 15, 2021 - 11:52 pm

“Again, It is not a good practice to change Bash prompt name”

Could you clarify? I have been using highly customized PS1 for years. I’m not sure I understand what your warning is for.

Reply
sk January 16, 2021 - 10:52 am

If another shell like ‘zsh’ inherits your current shell, it will cause some problems.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More