Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Processing "%?" in PROMPT?



In my PROMPT, I want to get the status of the last command executed before the prompt, what in prompt expansion would be %?, but I want to pass it to a function.  (Why?  I like the idea of %? in the prompt, but thought it would be nice to dress it up a bit by rendering it as something other than just a number: “EX_USAGE” instead of “64” or “SIGILL” instead of “132”.  That means passing the value of %? to a function to generate the text that appears in the prompt.)

My first attempt was to set PROMPT to ‘$(pretty-status %?)’, which doesn’t work because the “%?” is passed literally.  I’ve tried various ways of evaluating “%?” — “print -P %?”, the “(%)” expansion flag — but none them work, because they expand to the same thing as “$?”, which is the status of the last command executed anywhere, including inside command substitution for the prompt.  I could use a precmd function to save $? in a global, but only if I could guarantee that it was called before anything else, and that’s not a promise I’m comfortable making.

Any suggestions?  zsh clearly has the value I want, since it uses it in prompt percent expansion, but how do I get at it?  I’m honestly not sure if the behavior I’m seeing in “print -P %?” is a bug or if it’s working as designed.  I’m mostly using zsh 5.3, but I’m seeing the same thing in 5.9.


—Chris N.


Messages sorted by: Reverse Date, Date, Thread, Author