[LRUG] Last exit status in bash prompt

Jim Myhrberg contact at jimeh.me
Mon Jan 9 06:28:47 PST 2012


Not a direct solution to your problem, but I'd recommend you switch from bash to zsh and use the oh-my-zsh (https://github.com/robbyrussell/oh-my-zsh) project. Lots of the themes (prompt styles) support the exit status right out of the box. And having done the switch myself a couple of months ago, I highly recommend it.

On Monday, 9 January 2012 at 14:20, Andrew Stewart wrote:

> Hola El Rug,
> 
> I'm having trouble incorporating the exit status of the last command into my bash prompt. My prompt executes a couple of commands to display the current Ruby version and the current git status, and then I want to use the exit status of the last command to control the colour of the final character (e.g. $).
> 
> However the prior commands (for the Ruby and git info) make the last-exit-status test useless so I need to save out the value at the start of the prompt. However I cannot seem to make it work.
> 
> For example, this shows the correct exit status:
> 
> PS1="\$? \$(rbenv version-name) $ "
> 
> But I want to make use of the exit status at the end of the prompt, along these lines:
> 
> PS1="XXX \$(rbenv version-name) \$( if [ YYY -eq 0 ]; then echo $GREEN; else echo $RED; fi ) $ "
> 
> where:
> - XXX means save $?
> - YYY means retrieve what XXX saved
> - GREEN and RED are variables defined earlier in my bashrc with Bash's wacky colour codes.
> 
> I'm sure there's a simple answer but I can't find it!
> 
> Thanks in advance,
> 
> Andy Stewart
> -----
> http://airbladesoftware.com
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org (mailto:Chat at lists.lrug.org)
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20120109/37632c0c/attachment-0003.html>


More information about the Chat mailing list