I'm trying to set up a bash prompt for - hostname(screen#):directory$ which is coloured green if the last command completed successfully, red if not. This is what I have so far, which actually does the job but seems to cause display problems if the command wraps a line:
PS1="\[\`if [[ \$? = "0" ]]; then echo '\e[32m'; else echo '\e[31m' ; fi\` - \h(${WINDOW}):\W$\e[00m "
Googling the issue I found this helpful SO post with a comment that mentions wrapping nonprinting characters in \\[ and \\] to avoid this issue. Therefore I tried the following, but it did not solve the issue, and furthermore breaks the colour change:
PS1="\[\`if [[ \$? = "0" ]]; then echo '\e[32m'; else echo '\e[31m' ; fi\`\] - \h(${WINDOW}):\W$\[\e[00m\] "
How can I keep the structure of this prompt, with colours, but fix it so that long commands are displayed properly?

unexpected EOF while looking for matching \)'on the line that defines the function. Near as I can tell it seems that bash thinks that there is an unclosed(` in the do line, but I really don't see how that could be. – dotancohen Nov 20 '12 at 22:07PS1, which got two tabs. – dotancohen Nov 20 '12 at 22:26wget http://pastie.org/pastes/5408715/download -O env.sh– Gilles Quénot Nov 20 '12 at 22:37for loopin favour of(( $retval + ${PIPESTATUS[@]/%/+} + 0))using bash parameter expansion – Gilles Quénot Nov 21 '12 at 08:04$? = "0"? Also, if I might ask, where is thecat<<-'EOF'value used, and for what purpose is theEOFat the end of the function? I'm new to bash scripting and I've tried googling these questions but I seem to be missing for which keywords to google on. – dotancohen Nov 21 '12 at 10:11PROMPT_COMMANDas an answer just to make it easier to read. – Nikos Alexandris Apr 06 '15 at 11:07