[LRUG] Last exit status in bash prompt

Graeme Mathieson mathie at woss.name
Mon Jan 9 06:43:05 PST 2012


Hi,

I missed the original email, but here's the bit of my ~/.bashrc that sets the prompt:

# Pretty prompt
smiley() {
  if [ $? = 0 ]; then
    printf $1
  else
    printf $2
  fi
  return $?
}

export GIT_PS1_SHOWDIRTSTATE="true"
export GIT_PS1_SHOWSTASHSTATE="true"
export GIT_PS1_SHOWUNTRACKEDFILES="true"
export PS1='$(smiley "\[\033[01;32m\]:)\[\033[00m\]" "\[\033[01;31m\]:(\[\033[00m\]") \[\033[01;34m\]\W\[\033[00m\] \[\033[01;36m\]$(rvm-prompt i v g)\[\033[00m\]$(__git_ps1 " \[\033[01;35m\](%s)\[\033[00m\]") \$ '

The colour escapes are pretty horrific, but the key bit is that PS1 evaluates `$(smiley ":)" ":(")` every time it's displayed and chooses one of the arguments depending on the last status code.

I'm sure I stole it from someone else, but I can't remember who to credit, sorry.

Cheers,

Graeme.

On 9 Jan 2012, at 14:36, Viktor Tron wrote:

> i guess you simply need something like this. hth
> 
> function exitstatusprompt {
> EXITSTATUS="$?"
> #PROMPT=$(rbenv version-name)
> if [ "${EXITSTATUS}" -eq 0 ]
>    then
>       PS1="${PROMPT} ${GREEN}"
>    else
>       PS1="${PROMPT} ${RED}"
> fi
> }
> PROMPT_COMMAND=exitstatusprompt
> 
> 
> On Mon, 09 Jan 2012 14:20:16 -0000, Andrew Stewart <boss at airbladesoftware.com> 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
>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org

-- 
Graeme Mathieson

Follow me on Twitter: <http://twitter.com/mathie>
Blog: <http://woss.name/>
Telephone: +44 (0)7949 0777 44 



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


More information about the Chat mailing list