我正在尝试在 Red Hat Enterprise Linux 6 计算机上使用自定义 PS1 行,包括颜色和 git 存储库信息。我有一个预定义版本,可以在运行 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> '
更新二
输出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
更新三
我的完整.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=""