[LRUG] Last exit status in bash prompt
Gareth Humphries
gareth1 at erozen.org
Mon Jan 9 13:39:13 PST 2012
> Date: Mon, 9 Jan 2012 15:20:16 +0100
> From: Andrew Stewart <boss at airbladesoftware.com>
> To: London Ruby Users Group <chat at lists.lrug.org>
> Subject: [LRUG] Last exit status in bash prompt
> Message-ID:
> <633E9F1E-1145-4E51-8DEA-B96D76B3FF77 at airbladesoftware.com>
> Content-Type: text/plain; charset=us-ascii
>
> 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
>
>
It would be easy to make the whole prompt red/green, by including the RC
check as the very first part of PS1:
PS1='`if [ $? == 0 ] ; then echo TRUE ; else echo FALSE ;fi`'" \u@\h -
\w\n`ruby -v`\$ "
Or, you could set another var RUBYVERSION in .bashrc and make use of that -
down side is that it will only ever represent the version of ruby at the
time the shell was started.
As an even uglier but complete solution, you can use the var as above, and
then set the contents of it at the end of the PS1 string - so the version
in the prompt will represent the version as when the last-but-one command
completed:
PS1='$RUBYVERSION `if [ $? == 0 ] ; then echo TRUE ; else echo FALSE
;fi`'"\n\u@\h - \w\$ " RUBYVERSION="`ruby -v`"
Not attractive bash, but anyway.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20120110/518653ae/attachment.html>
More information about the Chat
mailing list