Bash: Display exit status in prompt:

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash interprets the special prompt escapes only before expanding any variables in the prompt. This means that using \e in a variable that is expanded from the prompt doesn't work, even though it does work directly in PS1.

For example, this works as expected, and gives red text:

PS1='\e[1;31m this is in red '

But this doesn't, it just puts a literal \e in the prompt:

RED='\e[1;31m'
PS1="$RED not in red "

If you want to store the color escapes in variables, you can use ANSI-C quoting ($'...') to put a literal escape character in the variable.

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) $?\[${NONE}\] '

You can't use get_exit_status properly here, since its output contains both printing (the exit code) and non-printing characters (the color codes), and there's no way to mark it correctly in the prompt. Putting \[...\] would mark it as non-printing in full, which is not correct. You'll have to change the function so that it only prints the proper color-code, and then surround it with \[...\] in the prompt.


When you run PS1='${RED}\h $(get_path) ${exitStatus}${NONE} ', the PS1 variable is set to ${RED}\h $(get_path) ${exitStatus}${NONE}, where only \h is a prompt escape sequence. After the prompt sequences are expanded (yielding ${RED}darkstar $(get_path) ${exitStatus}${NONE}), the shell performs the usual expansions such as variable expansions. You get a displayed prompt that looks like \e[1;31mdarkstar PATH 0\e[m. Nothing along the way expands the \e sequences to actual escape characters.

When you run PS1="${RED}\h $(get_path) ${exitStatus}${NONE} ", the PS1 variable is set to \e[1;31m\h PATH 0\e[m. The variables RED, exitStatus and NONE are expanded at the time of the assignment. Then the prompt contains three prompt escape sequences (\e, \h, and \e again). There are no shell variables to expand at this stage.

In order to see colors, you need the color variables to contain actual escape characters. You can do it this way:

RED=$'\033[1;31m'
NONE=$'\033[m'
PS1='\[${RED}\]\h \w $?\[${NONE}\] '

$'…' expands backslash-octal sequences and some backslash-letter sequences such as \n, but not including \e. I made three other changes to your prompt:

  • Use \[…\] around non-printing sequences such as color-changing commands. Otherwise your display will end up garbled because bash can't figure out the width of the prompt.
  • \w is a built-in escape sequence to print the current directory.
  • You don't need anything complicated to show $? in the prompt if you don't have a PROMPT_COMMAND in the first place.

Tags:

Bash

Prompt