Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When running a bash script, I would like to be able to tell if it is running within a screen. Is this possible?

I want this so that .bashrc can set the colour of the command prompt depending on whether the sessions is started within a screen or not.

Discovered this link once mine was answered: How can I tell whether I'm in a screen?

share|improve this question
    
Ah, I see. My $TERM is "screen" if I'm in screen, otherwise "xterm". –  DustByte Aug 15 '13 at 15:36
    
In fact, my exact question was answered already on stackoverflow. I've added a link to it. –  DustByte Aug 15 '13 at 15:39
    
@Bernhard Thanks for pointing that out. I'm not going for the rank though. Since this was "one of the approaches" I thought there might be a better answer to the question. –  val0x00ff Aug 16 '13 at 7:08
    
@Bernhard I will keep that in mind! Thanks again. –  val0x00ff Aug 16 '13 at 7:16
    
@slm, Can anybody explain how that's a duplicate question to the one linked? –  Stéphane Chazelas Jan 8 at 19:09
show 2 more comments

3 Answers 3

up vote 5 down vote accepted

Typically, $STY will be set.

So:

if [ -n "$STY" ]; then
  echo "I'm most likely running under screen"
fi

$STY is typically what you need to talk to your screen. That is used to construct the path of the Unix domain socket used to control screen (something like /var/run/screen/S-$USER/$STY.

Now, that won't work for instance if from that screen, you ssh to another machine. The shell started there won't have $STY in its environment, and that wouldn't be of any use to it anyway, since it wouldn't be able to access the Unix domain sockets on your machine.

However, it is possible to query the terminal with an escape sequence to determine its type:

if [ -t 1 ] && [ -t 0 ]; then
  s=$(stty -g)
  stty -icanon -echo min 0 time 3
  printf '\033[>c'
  type=$(dd count=1 2> /dev/null)
  stty "$s"
  case $type in
    (*'>83;'*) echo "this is screen"
  esac
fi

Another approach, as suggested by @val0x00ff is to check the value of the $TERM environment variable. That value is meant to tell applications what type of terminal they're talking to. It is set by screen to something that starts with screen (as screen can implement different variants of terminals). $TERM is passed accross rlogin, rsh, telnet, ssh. It's not as guaranteed to work as the above but is simpler and less intrusive.

case $TERM in
  (screen*) echo "I'm more than likely running in screen"
esac

Like for the previous solution, you may be running in screen, but you may not be able to issue commands to it with screen -X for instance. However note, that you can pass commands using escape sequences (though it's not enabled by default for security reasons).

share|improve this answer
add comment

When running in screen your $TERM environment variable changes to screen. You can check in the script e.g

if [[ "$TERM" == screen* ]]; then
 echo "Running in screen"... ; 
 else 
   echo "Outside screen"; 
fi 

That would be one of the approaches. echo $TERM outside the screen and again echo $TERM inside the screen and see the difference

share|improve this answer
add comment

I use tmux myself, and this little bit to list sessions only if im not already in a tmux session

if [ -z "$TMUX" ]; then
    alias tmuxa='tmux attach -d -t'
    alias tmuxc='tmux new-session -s'
    echo "You're not in a tmux session, create a new one with tmuxc, or attach to one of the ones below with tmuxa"
    tmux ls
fi
share|improve this answer
add comment

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.