.bashrc의 PS1이 다른 곳에서 덮어쓰기되었습니다.

.bashrc의 PS1이 다른 곳에서 덮어쓰기되었습니다.

색상 및 Git 저장소 정보를 포함하여 Red Hat Enterprise Linux 6 시스템에서 사용자 정의 PS1 라인을 사용하려고 합니다. Ubuntu 또는 Mint를 실행하는 다른 시스템에서 성공적으로 작동하는 사전 정의된 버전이 있습니다.

광산에서는 .bashrc맨 아래에 다음 섹션을 추가했습니다.

# Colors
Black='\e[0;30m'        # Black
Red='\e[0;31m'          # Red
...
NC="\e[m"               # Color Reset

# show git branch
parse_git_branch() {
  # git branch 2> /dev/null | sed -e '/^\[^*\]/d' -e 's/* \(.*\)/|\1/'
  git rev-parse --abbrev-ref HEAD 2> /dev/null | sed 's/^/|/g'
}
PS1="\[$Green\]\u@\h \[$BBlack\]\w\[$Yellow\]\$(parse_git_branch)\[$NC\] $ "

그러나 터미널을 열면 여전히 기본 PS1 라인이 표시됩니다. 인쇄물 .echo $PS1\[\033]0;\u@\h: \w\007\]\u@\h:\w>분명히 이 변수는 어딘가에서 재정의되었습니다. 하지만 어디서, 어떻게 찾을 수 있나요?

그런데 .bashrc실행은 확실합니다. 라인을 추가 echo "hello"하고 터미널을 열었을 때 결과를 확인하여 이를 확인했습니다 .

고쳐 쓰다

실행하면 bash -x많은 출력이 인쇄됩니다.

+ On_White='\e[47m'
+ NC='\e[m'
+ PS1='\[\e[0;32m\]\u@\h \[\e[1;30m\]\w\[\e[0;33m\]$(parse_git_branch)\[\e[m\] $ '
++ PS1='\[\033]0;\u@\h: \w\007\]\u@\h:\w> '

업데이트 2

출력 grep -H PS1 ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login ~/.bash_aliases /etc/bash.bashrc /etc/profile /etc/profile.d/* /etc/environment 2> /dev/null:

/home/myself/.bashrc:# this does not apply, but PS1 env var is empty.
/home/myself/.bashrc:[ -n "$PS1" ] || INTERACTIVE=0
/home/myself/.bashrc:PS1="\[$Green\]\u@\h \[$BBlack\]\w\[$Yellow\]\$(parse_git_branch)\[$NC\] $ "
/etc/profile.d/company.sh:  linux:root) PS1="\u@\h:\w# "; TMOUT=3600 ;;
/etc/profile.d/company.sh:  linux:*)    PS1="\u@\h:\w> " ;;
/etc/profile.d/company.sh:  *:root)     PS1="\[\033]0;\u@\h: \w\007\]\u@\h:\w# "; TMOUT=3600 ;;
/etc/profile.d/company.sh:  *)          PS1="\[\033]0;\u@\h: \w\007\]\u@\h:\w> " ;;
/etc/profile.d/company.sh:export PS1
/etc/profile.d/colorls.sh:  [ -z "$PS1" ] && return

3개 업데이트

내 완료 .bashrc:

PKG_ROOT=/opt/companyhome/

NFS_ROOT=/share/install/companyhome/current/

LINKS_VERSION=3.0.0.0

# write to stdout (disabled for non interactive (e.g. scp) logins)
print_msg() {
    if  [ "$INTERACTIVE" = "1" ]; then
        echo "$1"
    fi
}

print_msg_debug(){
    if [[ ! -z "$COMPANYHOME_INIT_DEBUG" ]]; then
        print_msg "$@"
    fi;
}

# check if this is an interactive session.
# tty results with 1 if not terminal. But with ansible remote execution,
# this does not apply, but PS1 env var is empty.
INTERACTIVE=1
tty -s || INTERACTIVE=0 
[ -n "$PS1" ] || INTERACTIVE=0


print_msg_debug "loading companyhome"

# define_companyhome_root
# check if we run against a packaged version or a nfs (legacy) version of companyhome 
CURRENT_ROOT=""
if [ -d "$PKG_ROOT" ]; then
    CURRENT_ROOT=$PKG_ROOT
elif [ -d "$NFS_ROOT" ]; then
    CURRENT_ROOT=$NFS_ROOT
else 
    print_msg "Error no companyhome installation found."
    print_msg "Companyhome could not be loaded."
    return 1
fi 

export "COMPANYHOME_ROOT=$CURRENT_ROOT"
print_msg_debug "companyhome is installed in \"$CURRENT_ROOT\""

# include companyhome
. "${COMPANYHOME_ROOT}/update/check_linksversion"
. "${COMPANYHOME_ROOT}/bashrc_company"

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi



    # Normal Colors
Black='\e[0;30m'        # Black
Red='\e[0;31m'          # Red
Green='\e[0;32m'        # Green
Yellow='\e[0;33m'       # Yellow
Blue='\e[0;34m'         # Blue
Purple='\e[0;35m'       # Purple
Cyan='\e[0;36m'         # Cyan
LightGray='\e[0;37m'    # Light Gray

# Bold
BBlack='\e[1;30m'       # Black
BRed='\e[1;31m'         # Red
BGreen='\e[1;32m'       # Green
BYellow='\e[1;33m'      # Yellow
BBlue='\e[1;34m'        # Blue
BPurple='\e[1;35m'      # Purple
BCyan='\e[1;36m'        # Cyan
BWhite='\e[1;37m'       # White

# Background
On_Black='\e[40m'       # Black
On_Red='\e[41m'         # Red
On_Green='\e[42m'       # Green
On_Yellow='\e[43m'      # Yellow
On_Blue='\e[44m'        # Blue
On_Purple='\e[45m'      # Purple
On_Cyan='\e[46m'        # Cyan
On_White='\e[47m'       # White

NC="\e[m"               # Color Reset


# show git branch
parse_git_branch() {
  # git branch 2> /dev/null | sed -e '/^\[^*\]/d' -e 's/* \(.*\)/|\1/'
  git rev-parse --abbrev-ref HEAD 2> /dev/null | sed 's/^/|/g'
}
PS1="\[$Green\]\u@\h \[$BBlack\]\w\[$Yellow\]\$(parse_git_branch)\[$NC\] $ "

${COMPANYHOME_ROOT}/bashrc_company:

INTERACTIVE=1
tty -s || INTERACTIVE=0 
[ -n "$PS1" ] || INTERACTIVE=0

# is_nfs_home returns 0 (success) if /home is nfs/network based, else 1 (local home)  
# the function does not guarantee the accessibility
is_nfs_home(){
    # if $HOME is an explicit mount -> nfs else its local
--
        export PS2='> '
    fi
}
sp () {
    setps2
    export PROMPT_COMMAND='PS1=`echo "\u@\h$PS2"`'
    export PS1
}
dp () {
    setps2
    if [ "$TERM" = "dtterm" ] || [ "$TERM" = "xterm" ] || [ "$TERM" = "xterm-color" ] || [ "$TERM" = "linux" ]; then
        export PROMPT_COMMAND='PS1="\[\033]0;\u@\h: \w\007\]\u@\h:\w$PS2"'
    elif [ "$TERM" = "sun-cmd" ] || [ ! -z $EMACS ] ; then
        export PROMPT_COMMAND='PS1=`echo "\u@\h:\w$PS2"`'
    else
        sp
    fi
    export PS1
}

dp
export ignoreeof=0

답변1

문제는 파일 ${COMPANYHOME_ROOT}/bashrc_company의 다음 줄 에 있습니다.

export PROMPT_COMMAND='PS1=`echo "\u@\h$PS2"`'

PROMPT_COMMAND변수는 프롬프트가 표시되기 전에 실행해야 하는 명령을 정의합니다. 귀하의 경우 PS1 설정으로 설정되었습니다. 따라서 프롬프트가 표시될 때마다 PS1이 기본값으로 재설정됩니다.

왜 누군가가 이것을 하고 싶어하는지 모르겠지만 고치는 것은 쉽습니다. 해당 줄을 제거 하거나 다음에서 다른 것으로 ${COMPANYHOME_ROOT}/bashrc_company설정하십시오 .PROMPT_COMMAND~/.bashrc

PROMPT_COMMAND=""

관련 정보