0

I am trying to color my prompt based on the return code of the previous command. I have the following:

PROMPT='%{$fg_bold[cyan]%}%~$(git_prompt_info) %($?.%{fg_bold[cyan]%}.%{$fg_bold[red]%})»%{$reset_color%} '

Specifically I'm having trouble with the %($?.%{fg_bold[cyan]%}.%{$fg_bold[red]%}) portion.

Unfortunately my prompt ends up looking like this:

enter image description here

What am I doing wrong?

EDIT:

After fixing my typo I now have:

PROMPT='%{$fg_bold[cyan]%}%~$(git_prompt_info) %($?.%{$fg_bold[cyan]%}.%{$fg_bold[red]%})»%{$reset_color%} '

My prompt now looks like this:

enter image description here

carloabelli
  • 4,289
  • 3
  • 43
  • 70

2 Answers2

1

You forgot the $ for the parameter expansion, and you don't precede the ? with a dollar; it's a flag for the conditional, not a parameter reference.

%(?.%{$fg_bold[cyan]%}.%{$fg_bold[red]%})
  ^   ^
chepner
  • 497,756
  • 71
  • 530
  • 681
1

An alternative would be to send the return code (when different than 0) to kill -l so that you can display the name of the error code.

This is what I use (for the prompt at the right side)

setopt PROMPT_SUBST
RPS1='%(?..%{$fg_bold[red]%}[%? $(kill -l $?)]%{${reset_color}%} )%T' # time
Francisco
  • 3,980
  • 1
  • 23
  • 27
  • Definitely interesting and I'll think about it, but my preference is more subtlety in my prompt. Thanks for sharing though – carloabelli Sep 03 '15 at 13:58