Colored PS1 string
- by Will Vousden
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?