0

Some variables aren't showing up, in this case, this PS_GIT variable

#!/usr/bin/bash

# Other codes
# ...

export USER=""
export HOSTNAME=""

RESET="\[\033[0m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
YELLOW="\[\033[0;33m\]"

function parse_git_branch {
    PS_BRANCH=""

    if [ -d .svn ] ; then
        PS_BRANCH="(svn r$(svn info|awk '/Revision/{print $2}'))"
        return
    elif [ -f _FOSSIL_ -o -f .fslckout ]; then
        PS_BRANCH="(fossil $(fossil status|awk '/tags/{print $2}')) "
        return
    fi

    ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    PS_BRANCH="(git ${ref#refs/heads/}) "
}

PROMPT_COMMAND=parse_git_branch

PS_INFO="${GREEN}${USER}${RESET}@${GREEN}${HOSTNAME}${RESET}:${BLUE}\w"
PS_GIT="${YELLOW}${PS_BRANCH}"
PS_TIME=" ${RED}[\t]${RESET}"

export PS1="\[\033[0G\]${PS_INFO} ${PS_GIT}${PS_TIME}\n${RESET}\$ "

When in a directory, I ran echo $PS_BRANCH and it showed (git main) so I know that the variable work. But, it's not showing at the prompt

Screenshot example

Askoi
  • 1
  • 1
  • 1
    Does this answer your question? [BASH print result of linux command in PS1 variable](https://stackoverflow.com/questions/60000157/bash-print-result-of-linux-command-in-ps1-variable) – dibery Jun 14 '21 at 05:36
  • `export PS1='$PS_BRANCH'` should work. (Add back other element later on your own.) – dibery Jun 14 '21 at 05:37
  • @dibery That's just a [useless use of `echo`.](http://www.iki.fi/era/unix/award.html#echo) – tripleee Jun 14 '21 at 05:38
  • @tripleee oh yes, edited. Thanks. – dibery Jun 14 '21 at 05:40
  • PS1 expands the variable when it's defined. so you'd have to change PS1 each time in your PROMPT_COMMAND – smemsh Jun 14 '21 at 05:48
  • Apparently, `PS_GIT="$YELLOW$PS_BRANCH"` worked. I changed it from `PS_GIT="${YELLOW}${PS_BRANCH}"` to `PS_GIT="$YELLOW$PS_BRANCH"` and it worked – Askoi Jun 14 '21 at 05:56

0 Answers0