Linux screen设置tab title被重置的问题

在某些Linux机器上,开启screen后,当设置了tab的title(Ctrl+A,A)后,输入任意命令,tab title会被重置为机器名称。

之前在某厂实习的时候,也遇到了相同的问题,一直以为是screen配置的问题。

其实,这是bash的配置。

看~/.bashrc:

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

而在/etc/bashrc中,有以下几行:

# are we an interactive shell?
if [ "$PS1" ]; then
    case $TERM in
    xterm*)
        if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
        else
            PROMPT_COMMAND='echo -ne "3]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "
# are we an interactive shell?
if [ "$PS1" ]; then
    case $TERM in
    xterm*)
        if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
        else
            PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
        fi
        ;;
    screen)
        if [ -e /etc/sysconfig/bash-prompt-screen ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
        else
        PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\033\\"'
        fi
        ;;
    *)  
        [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
        ;;
    esac
    # Turn on checkwinsize
    shopt -s checkwinsize
    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\H \W]\\$ "
fi
7"' fi ;; screen) if [ -e /etc/sysconfig/bash-prompt-screen ]; then PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen else PROMPT_COMMAND='echo -ne "3_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "3\\"' fi ;; *) [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default ;; esac # Turn on checkwinsize shopt -s checkwinsize [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\H \W]\$ " fi

不难发现,若检测当前为screen的时候,强制写入了shell的标题为机器名,而这个值的设定,与screen tab的title实际是冲突的,因此,我们只要把screen的这个switch注视掉,就好了。

如下:

# are we an interactive shell?
if [ "$PS1" ]; then
    case $TERM in
    xterm*)
        if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
        else
            PROMPT_COMMAND='echo -ne "3]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "
# are we an interactive shell?
if [ "$PS1" ]; then
    case $TERM in
    xterm*)
        if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
        else
            PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
        fi
        ;;
    #screen)
    #   if [ -e /etc/sysconfig/bash-prompt-screen ]; then
    #       PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
    #   else
    #       PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\033\\"'
    #   fi
    #   ;;
    *)  
        [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
        ;;
    esac
    # Turn on checkwinsize
    shopt -s checkwinsize
    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\H \W]\\$ "
fi
7"' fi ;; #screen) # if [ -e /etc/sysconfig/bash-prompt-screen ]; then # PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen # else # PROMPT_COMMAND='echo -ne "3_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "3\\"' # fi # ;; *) [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default ;; esac # Turn on checkwinsize shopt -s checkwinsize [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\H \W]\$ " fi

这个我猜测不是管理员配置的,在CentOS、RHEL等同源发行版上,都有类似的配置。

One thought on “Linux screen设置tab title被重置的问题

  1. Anonymous

    确实有用,好像高版本就出这个问题。。。 screen研究好久没弄明白~

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *