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](https://stackoverflow.com/a/103874/343302) 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?