This works for me:
__myretval () {
if (($1)); then
printf -- "%s" "$1"
fi
}
PS1='$(__myretval "$?") $'
It seems your problem was with the quotes.
When you state:
PS1="$(__myretval '$?') $"
what you're doing is (because of the double quotes): setting PS1
to the output of the function __myretval
with the argument '$?'
, where $?
is expanded now. So your PS1
never changes.
What you want instead is PS1
to contain the string:
$(__myretval "$?") $
so that this string is expanded (evaluated) at each new prompt. That's why you should use single quotes to define your PS1
.