Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

The "screen" refers to a program mentioned in How to reconnect to a disconnected ssh session . That is a good facility.

But there is a question I'd really like to know. How do I know whether I'm running inside a "screen"? The difference is:

  • If yes, I know I can safely close current terminal window, e.g., close a PuTTY window, without losing my shell(Bash etc) session.
  • If no, I know I have to take care of any pending works before I close the terminal window.

Better, I'd like this status to be displayed in PS1 prompt so that I can see it any time automatically.

share|improve this question
3  
If you use tmux instead of screen, you get a little status bar by default. They're pretty much the same for basic usage. –  Samuel Edwin Ward Apr 6 '12 at 20:35
7  
Place your totem on the table, and let it spin. If it keeps spinning, you're OK. Else you're in a screen :] –  Konerak Apr 7 '12 at 11:09
    
You can also use byobu with screen and tmux. It can be setup to load automatically at login and then hitting F6 key disconnects and logs out. –  mp3foley Apr 12 '12 at 20:58

8 Answers 8

up vote 60 down vote accepted

(Stolen from "How can I tell whether I'm in a screen?" over on StackOverflow and authored by user jho. P.S. You can't vote for a duplicate across StackExchange sites.)

Check $STY. If it's null, you're on a "real" terminal. If it contains anything, it's the name of the screen you're in. If you use tmux instead of screen, also check $TMUX. To add this to your prompt, add the following to your ~/.bashrc:

if [ -n "$STY" ]; then export PS1="(screen) $PS1"; fi
if [ -n "$TMUX" ]; then export PS1="(tmux) $PS1"; fi
share|improve this answer

Well, most of the time (absent someone's attempts to screw with things) your TERM will be set to screen (or at least mention screen somewhere).

The easy solution to the problem is just run screen everywhere. Don't leave home without it, I say.

share|improve this answer

Look for $STY which provides details that screen uses to communicate with itself; $WINDOW will then be the current screen window number.

share|improve this answer

The simple check I usually use is to just hit Ctrl-a:

  • If the cursor jumps to the start of the line, I'm not inside a screen session.

  • If nothing happens, I know that I'm inside a screen session and that I've just used the screen control key. I then hit a (jump to the beginning of the line), w (show current screen windows) or execute some other "harmless" screen command to get back to the command prompt.

(Of course, this only works if you are currently executing bash or some other piece of software that "jumps to the start of the line" or does something equivalently harmless when hitting Ctrl-a.)

share|improve this answer
3  
This only works if your screen control key is left at the default Ctrl-a, of course. –  ShreevatsaR Apr 6 '12 at 12:53
1  
hitting control key + DD would detach and terminate ssh. You can hit that combination for more convenience –  akostadinov Apr 6 '12 at 14:14
    
While the answer is fine for the actual question, this method is not scriptable. –  Nikodemus Dec 2 at 11:08

you could also recurse up the process tree from $$ to see if you find a screen instance. the exact chain will probably depend on your setup--on my OS X box, a zsh inside screen has login as its parent, SCREEN as its grandparent, and screen as its great-grandparent.

share|improve this answer

I keep this in my .bashrc:

 PS1='[\u@\h \W'
 if [ "$WINDOW" ]; then PS1="$PS1 ($WINDOW)"; fi
 PS1="$PS1]$"

It's not foolproof, but whenever I create a new screen, it puts the window number in the prompt. If I'm not running screen, there's no number.

share|improve this answer

I have an even cooler way.

Add the following to your ~/.screenrc and you'll see a status line down the bottom (showing in inverted text the date, time and system load. More escape codes found in man page link below.

    hardstatus alwayslastline
    hardstatus string '%Y-%m-%d %c | %l'

Source this when in a screen session with ctrl-a:source ~/.screenrc.

I have to thank Kyle Rankin of Linux Journal for this. See his fantastic article at http://www.linuxjournal.com/article/10950. The man page section he refers to can be found at http://www.gnu.org/software/screen/manual/screen.html#String-Escapes.

share|improve this answer

You can use this. It's short and sweet.

function trim() {
    echo "${1// /}"
}

VAR="    xxx    "
echo $VAR
TRIMMED_VAR=$(trim "$VAR")
echo $TRIMMED_VAR
share|improve this answer
    
How is this even relevant here? –  Michael Hampton Nov 14 '13 at 13:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.