0

I have a file named .ps1, and I'm running source .ps1 in bash to try and set my prompt up with color. The contents of the .ps1 file are:

__prompt_command() {
    local EXIT="$?"
    local CLEAR='\[\e\[0m\]'
    local RED='\[\e\[1;31m\]'
    local GREEN='\[\e[1;32m\]'
    local CYAN='\[\e[1;33m\]'
    local BLUE='\[\e[1;34m\]'
    local MAGENTA='\[\e[1;35m\]'
    # bold
    local REDB='\[\e\[1;31m\]'
    local GREENB='\[\e[1;32m\]'
    local CYANB='\[\e[1;33m\]'
    local BLUEB='\[\e[1;34m\]'
    local MAGENTAB='\[\e[1;35m\]'

    local BGWHITE='\[\e47m\]'

    GIT_BRANCH="$(git branch --show-current 2>/dev/null)"
    if [ "$GIT_BRANCH" == "" ]; then
        GIT_BRANCH=" "
    else
        GIT_BRANCH=" (${GIT_BRANCH}) "
    fi
    if [ $EXIT != 0 ]; then
        PS1='[ ${GREENB}$?${CLEAR} ] ${BGWHITE}\t${CLEAR} ${CYAN}\w${CLEAR} ${MAGENTA}$GIT_BRANCH${CLEAR} ${RED}\u@\h${GREEN}\n\$${CLEAR} '
    else
        PS1='[ ${REDB}$?${CLEAR} ] ${BGWHITE}\t${CLEAR} ${CYAN}\w${CLEAR}${MAGENTA}$GIT_BRANCH${CLEAR}${RED}\u@\h${GREEN}\n\$${CLEAR} '
    fi
}
PROMPT_COMMAND=__prompt_command

When I run source .ps1 though, my prompt is entirely uncolored, but all the other features of my PS1 are working fine. A functioning command prompt with default color and background

What am I missing?

2 Answers 2

1

You use single quotes in the PS1= assignments, preventing variable expansion from happening right there at assignment time. For PS1 that's fine as Bash will do a second expansion pass later, immediately before it prints the prompt.

But that second pass will happen after the function ends (the PS1 isn't printed by or during the PROMPT_COMMAND; it is printed after), and because you've declared all those variables local to the function, they cease to exist when the function ends – so when they're finally needed they all expand to empty values (or wrong values in case of $?).

Anything you use from within PS1 with the delayed expansion needs to be a global variable. So you need to un-local practically everything in that function, and replace direct usage of $? with the stored $EXIT.

# These are constant so they can be declared once, outside the function
# If you want you can even 'readonly RED=...' to truly make them constant
CLEAR='\[\e[0m\]'
RED='\[\e[1;31m\]'

__prompt_command() {
    # Without 'local' the assignment will be global by default
    # You can use 'declare -g' to explicitly say "this is *deliberately* global”
    EXIT="$?"

In some cases I'd even use a function from within PS1, i.e. PS1='$(_print_cool_prompt)' and let that function directly 'echo' or 'printf' the prompt. (In such cases it would need to print \001..\002 around the color codes, not the usual \[..\].)

Also, you have too many backslashes in some of your color codes. The \[...\] are okay as they have special meaning around color codes. But that means there shouldn't be a \[ within the color code. It needs to be just \e[, not \e\[.

Also, try \e[91m for bright red that isn't bold – 1;31 means "bold + red", and in many terminals bold also makes the color bright, but most terminals support color codes 90+ to get bright colors without necessarily making the text bold.

(Also, the usual style convention is that local variables are lower-case. I'd say even global variables, when they aren't exported into the environment, seem to be better as lower-case.)

1
  • Thanks, I changed all ' characters to ". I also fixed my color codes to not use the `` character to only surround the codes, and it works great now! Commented Nov 2 at 22:33
0

@grawity's answer worked great! Thought I'd share the result and the code that made it: Fixed prompt colors

__prompt_command() {
    EXIT="$?"
    CLEAR="\[\e[0m\]"
    RED="\[\e[91m\]"
    GREEN="\[\e[32m\]"
    CYAN="\[\e[33m\]"
    BLUE="\[\e[34m\]"
    MAGENTA="\[\e[35m\]"
    # bold
    REDB="\[\e[1;91m\]"
    GREENB="\[\e[1;32m\]"
    CYANB="\[\e[1;33m\]"
    BLUEB="\[\e[1;34m\]"
    MAGENTAB="\[\e[1;35m\]"

    local BGWHITE="\[\e[47m\]"

    GIT_BRANCH="$(git branch --show-current 2>/dev/null)"
    if [ "$GIT_BRANCH" == "" ]; then
        GIT_BRANCH=" "
    else
        GIT_BRANCH=" (${GIT_BRANCH}) "
    fi
    PS1="${BGWHITE}\t${CLEAR} ${CYAN}\w${CLEAR} ${MAGENTA}$GIT_BRANCH${CLEAR} ${RED}\u@\h${GREEN}\n\$${CLEAR} "

    if [ $EXIT != 0 ]; then
        PS1="[ ${GREENB}${EXIT}${CLEAR} ] ${PS1}"
    else
        PS1="[ ${REDB}${EXIT}${CLEAR} ] ${PS1}"
    fi
}
PROMPT_COMMAND=__prompt_command

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.