Colored PS1 string

Posted by Will Vousden on Super User See other posts from Super User or by Will Vousden
Published on 2012-09-17T20:04:12Z Indexed on 2012/09/17 21:42 UTC
Read the original article Hit count: 243

Filed under:
|
|

Clarification: I want __foo to be executed each time the PS1 string is presented in the terminal, not when the PS1 string is constructed (hence its being in quotes). __foo contains logic that examines the current directory, so its execution must be deferred.


I'm trying to use different colours in my Bash PS1 string from within a Bash function:

LIGHTRED="\033[1;31m"
LIGHTGREEN="\033[1;32m"
RESET="\033[m"

__foo () {
    # Do some stuff and genereate a string to echo in different colours:
    echo -n '\['$1'\]firstcolour \['$2'\]secondcolour'
}

PS1='$(__foo '$LIGHTRED' '$LIGHTGREEN')\['$RESET'\] \$'

Essentially I want __foo to generate part of the PS1 in a bunch of different colours. My attempt doesn't seem to work, though; it produces the following output:

-bash: 31m: command not found
-bash: 32m: command not found
\[]firstcolour \[\]secondcolour $

What gives, and how can I fix it?

© Super User or respective owner

Related posts about bash

Related posts about shell