1

I'm trying to use a custom PS1 line, including colors and git repo information, on a Red Hat Enterprise Linux 6 machine. I have a predefined version I'm successfully using on other systems running Ubuntu or Mint.

In my .bashrc, I added the following part at the bottom:

# 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\] $ "

However, when opening a terminal, I still see the default PS1 line. A echo $PS1 prints \[\033]0;\u@\h: \w\007\]\u@\h:\w>. Apparently, this variable gets overridden somewhere. But where, or how can I find this out?

By the way, .bashrc definitely gets executed. I verified this by adding a line like echo "hello" and seeing the result when opening a terminal.

Update

Running bash -x prints a lot of output, ending with

+ 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> '

Update II

Output of 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

Update III

My full .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
  • 2
    Run bash -x. The output might be a bit much, but it should hint at what's happening. – muru Sep 17 '15 at 12:48
  • 1
    Please post the output of grep -H PS1 ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login ~/.bash_aliases /etc/bash.bashrc /etc/profile /etc/profile.d/* /etc/environment 2> /dev/null – terdon Sep 17 '15 at 12:52
  • @muru Thanks, I updated the question. To me, this looks like PS1 is indeed being overridden, or what exactly does the output say? – Cedric Reichenbach Sep 17 '15 at 12:54
  • That a file is being sourced within .bashrc, and it is setting PS1. – muru Sep 17 '15 at 12:56
  • @terdon Done. Looks interesting; I suppose company.sh overrides it then, right? – Cedric Reichenbach Sep 17 '15 at 13:00
2

The problem is this line in your ${COMPANYHOME_ROOT}/bashrc_company file:

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

The PROMPT_COMMAND variable defines a command that should be run before a prompt is shown. In your case, this has been set to setting PS1. So, each time a prompt is shown, your PS1 is being reset to the dwefault value.

I have no idea why anyone would want to do this, but it's simple enough to fix. Either delete that line from ${COMPANYHOME_ROOT}/bashrc_company or set PROMPT_COMMAND to something else in your ~/.bashrc:

PROMPT_COMMAND=""
  • Wow, that solved it, thank you very much! I chose the second solution, as I was afraid to fiddle around in a 2000-line script (bashrc_company, yes it's that big) from someone else... – Cedric Reichenbach Sep 17 '15 at 14:06
  • Changing PS1 in PROMPT_COMMAND makes sense when you want to the prompt to depend on dynamic information such as the current directory or the previously executed command. Here it would make sense if setps2 (not shown) sets PS2 to value that isn't constant in a given shell instance. – Gilles Sep 17 '15 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.