Estou tentando usar uma linha personalizada do PS1, incluindo cores e informações do Git repo, em uma máquina Red Hat Enterprise Linux 6. Eu tenho uma versão predefinida que estou usando com sucesso em outros sistemas que executam o Ubuntu ou o Mint.
No meu .bashrc
, adicionei a seguinte parte na parte inferior:
# 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/* \(.*\)/|/'
git rev-parse --abbrev-ref HEAD 2> /dev/null | sed 's/^/|/g'
}
PS1="\[$Green\]\u@\h \[$BBlack\]\w\[$Yellow\]\$(parse_git_branch)\[$NC\] $ "
No entanto, ao abrir um terminal, ainda vejo a linha PS1 padrão. Um echo $PS1
imprime \[3]0;\u@\h: \w.bashrc
7\]\u@\h:\w>
. Aparentemente, essa variável é substituída em algum lugar. Mas onde, ou como posso descobrir isso?
A propósito, echo "hello"
definitivamente é executado. Eu verifiquei isso adicionando uma linha como bash -x
e vendo o resultado ao abrir um terminal.
Atualizar
A execução de grep -H PS1 ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login ~/.bash_aliases /etc/bash.bashrc /etc/profile /etc/profile.d/* /etc/environment 2> /dev/null
imprime muita saída, terminando com
+ 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='\[3]0;\u@\h: \w/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="\[3]0;\u@\h: \wPKG_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/* \(.*\)/|/'
git rev-parse --abbrev-ref HEAD 2> /dev/null | sed 's/^/|/g'
}
PS1="\[$Green\]\u@\h \[$BBlack\]\w\[$Yellow\]\$(parse_git_branch)\[$NC\] $ "
7\]\u@\h:\w# "; TMOUT=3600 ;;
/etc/profile.d/company.sh: *) PS1="\[3]0;\u@\h: \wINTERACTIVE=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="\[3]0;\u@\h: \w# 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/* \(.*\)/|/'
git rev-parse --abbrev-ref HEAD 2> /dev/null | sed 's/^/|/g'
}
PS1="\[$Green\]\u@\h \[$BBlack\]\w\[$Yellow\]\$(parse_git_branch)\[$NC\] $ "
7\]\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
7\]\u@\h:\w> " ;;
/etc/profile.d/company.sh:export PS1
/etc/profile.d/colorls.sh: [ -z "$PS1" ] && return
7\]\u@\h:\w> '
Atualização II
Saída de .bashrc
:
+ 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='\[3]0;\u@\h: \w/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="\[3]0;\u@\h: \wPKG_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/* \(.*\)/|/'
git rev-parse --abbrev-ref HEAD 2> /dev/null | sed 's/^/|/g'
}
PS1="\[$Green\]\u@\h \[$BBlack\]\w\[$Yellow\]\$(parse_git_branch)\[$NC\] $ "
7\]\u@\h:\w# "; TMOUT=3600 ;;
/etc/profile.d/company.sh: *) PS1="\[3]0;\u@\h: \wINTERACTIVE=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="\[3]0;\u@\h: \w%pre%7\]\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
7\]\u@\h:\w> " ;;
/etc/profile.d/company.sh:export PS1
/etc/profile.d/colorls.sh: [ -z "$PS1" ] && return
7\]\u@\h:\w> '
Atualização III
Meu total ${COMPANYHOME_ROOT}/bashrc_company
:
%pre%
%code% :
%pre%