-1

How to get current screen session name (bash)? I need implement it in Python, so would be much more better just command without Ctrl combinations.

I know way with screen -ls | grep 'Attached' but it's not elegant and sometimes not correct

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • See this [stackoverflow post](https://stackoverflow.com/questions/2479683/how-do-i-display-the-current-session-name-or-sockname-of-a-screen-session-in). Maybe it helps – Mihir Luthra Jul 21 '19 at 10:53
  • Possible duplicate of [How can I get currently attached screen session name?](https://stackoverflow.com/q/16848940/608639), [How to check screen is running?](https://stackoverflow.com/q/8015163/608639), [How can I tell whether I'm in a screen?](https://stackoverflow.com/q/5392618/608639), [Checking if a Screen of the Specified Name Exists](https://stackoverflow.com/q/12255388/608639), [How do I use the “screen” command if I don't know what the ps or tty the program came from?](https://unix.stackexchange.com/q/216003) – jww Jul 21 '19 at 16:10

1 Answers1

3

Screen stores the session name in the env var STY. So you can access it as os.environ['STY']

>>> import os
>>> os.environ['STY']
'17136.pts-3.suni-lnx'
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • If you also need to handle the case of not being in a screen session at all I'd recommend using `os.getenv('STY')` which will return `None` if the `$STY` variable doesn't exist in your environment – Peter Olson Feb 13 '20 at 23:40