Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to launch GNU Screen using my .bashrc. I'm almost there:

if [ -z "$STY" ]; then
   exec screen -dR
   exec gnome-terminal

This is wrong though! The first case works, screen launches when I open a terminal. But the second part fails. I want to open a regular terminal if I already have one open. But this just opens an infinite number of terminals...

share|improve this question

You could try (after the else):

if [ "$HAS_STARTED_TERM"!="1"]; then
    exec gnome-terminal
share|improve this answer

If I'm not mistaken, the terminal would open normally if you just remove the "else" block... Or maybe I'm missing something.

share|improve this answer
Well, the gnome-terminal probably hosts another copy of bash, which itself runs the .bashrc, and which in turn opens another gnome-terminal again... in an infinity of loops. – scraimer Apr 30 '09 at 20:51
Yes, that's my thinking, but it's not completely clear to me what devin is trying to accomplish. – Ivan Apr 30 '09 at 20:55

The way I have things set up I launch screen on every server I connect to if it has screen. When I disconnect and reconnect, the same screen session is reconnected. Sounds good so far, that's what everyone wants. But, if you have several windows open in screen, how do you disconnect without closing them? There are 2 options:

  1. My preferred ssh client will disconnect if I hit "`." and in my screenrc I use "autodetach", but that only works remotely.
  2. For local (or remote) screens I have my bashrc set to disconnect if I detach from screen. However, in rare cases you want to detach and stay connected. For that, I have some magic in my .bash_logout to complement my .bashrc magic.


hardstatus alwayslastline
hardstatus string '%{= kG} %{G}%H %{g}[%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}] %{W}%c %{g}'
#dynamic title
shelltitle '# |sh'

#When your ssh connection dies, screen is autodetached
autodetach on 

#terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I"
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm "Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm"
termcapinfo xterm-color "Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm"
# erase background with current bg color
defbce "on"


# set a fancy prompt (non-color, unless we know we "want" color)
if [[ $TERM =~ xterm-.*color || $TERM =~ screen.* ]]; then
   PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]# '
   #if [[ $TERM =~ screen.* ]]; then
   export SCREEN_CMD=$(which screen 2>/dev/null)
   if [[ ( $TERM =~ screen.* ) || ${SCREEN_CMD-X} != X && ${SCREEN_CMD-X} != "" ]]; then
      # This is the escape sequence ESC k \w ESC \
      #Use path as title
      #Use program name as title
      #Soliton@freenode#screen suggested screen -xRRS primary
      echo ^[k$(hostname|sed "s/\..*//")^[\\
      export SCREEN_CMD=$(which screen 2>/dev/null)
      if [[ ${SCREEN_CMD-X} != X && ${SCREEN_CMD-X} != "" ]]; then
         screen -xRRS primary && unset SCREEN_CMD && [[ $(stat -c %Y .screen_do_not_disconnect 2>/dev/null || stat -f %m .screen_do_not_disconnect 2>/dev/null) -gt 0 ]] || exit
   PS1='\u@\h:\w# '
   #Soliton@freenode#screen suggested screen -xRRS primary
   echo ^[k$(hostname|sed "s/\..*//")^[\\
   export SCREEN_CMD=$(which screen 2>/dev/null)
   if [[ ${SCREEN_CMD-X} != X ]]; then
      screen -xRRS primary && unset SCREEN_CMD && [[ $(stat -c %Y .screen_do_not_disconnect 2>/dev/null || stat -f %m .screen_do_not_disconnect 2>/dev/null) -gt 0 ]] || exit


# ~/.bash_logout

time="$(TZ=UTC date -d @0 2>/dev/null||date -r 0 +%Y%m%d%H%M)"
touch -d "$time" ~/.screen_do_not_disconnect 2>/dev/null || touch -t "$time" ~/.screen_do_not_disconnect

The scripts above work on both Linux and Mac OS X (which is the reason you see "2>/dev/null ||" in the stat, touch, and date commands). I also use a 256 color xterm and I believe all the supporting code is included here. Despite the length of this post, I have trim a lot out of the scripts/configs.

share|improve this answer

Your Answer


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.