0

Currently I have ! in the right prompt of as follows:

export RPS1="%B%F{red}!%f%b"

As I use , and command history is not being synced throughout its panes (unfortunately), the numbers are almost useless for me.

I tried to set RPS1 to ? and $? to display return code / error level of the command but with no success.

I remember I had to set:

setopt promptbang

for ! to be interpolated (interpreted, expanded).

How to achieve such a prompt on the right side of the command-line indicating the previous command's result in error number.

enter image description here

An example screenshot of my current prompt having >, >>, and ! as $PS1, $PS2, and $RPS1.

2 Answers 2

0

In order to show the return status of the previous command you can use the prompt escape %?.

RPS1='%B%F{red}%?%f%b'

You can even switch the color depending on the exit status:

RPS1='%B%(?.%F{green}.%F{red})%?%f%b'

Or only display anything, if the command returned with an error:

RPS1='%(?..%?)'

This will color the output green if %? evaluates to 0 and red otherwise.


In order to substitute arbitrary parameters (like $?) you would need to enable the PROMPT_SUBST option:

setopt promptsubst

In that case you need to sufficiently quote the parameter to prevent it from being substituted at declaration time. For example by using single quotes or by quoting $ with an \ inside double quotes.

RPS1='$?'
RPS1="\$?'

BTW: It is not necessary to export RPS1 (or any other shell configuration parameter) for it to take effect.

0

Just put ${?##0} into PS1 wherever you want a nonzero exit code to be displayed. If the exit code of the previous command is zero, it will be truncated to a null string (0 with all leading zeros removed is a null string).

user@host:~
$ bash
user@host:~
$ exit 4
logout
user@host:~ 4
$ 

In relevant part from my PS1, I have \e[31m${?##0}\n\[\e[$(((($?>0))*31))m\]. If the exit code from the previous command is nonzero, display it in red.

You must log in to answer this question.