$PROMPT_COMMAND (WAS: Re: Using $PROMPT_COMMAND to beep depending on success/failure)

Tim Chase blinux.list at thechases.com
Mon Apr 25 00:42:21 UTC 2016


> On 4/22/2016 2:09 PM, Lars wrote:
> Do any of you know of a way to get a number as the first cell(s)
> in the prompt indicating the error code of the previous command?

Yes, just include "$?" in your $PS1 variable.  I have something like:

  export PS1='{$?}\u@\h:\w\$ '

in my ~/.bashrc to give me a prompt like

  {0}tim at netbook:/etc$

where the "{0}" at the beginning shows the exit/error code from the
previously-run program.

I include the username ("\u"), short hostname ("\h"), and working
directory ("\w") mostly because I manage a number of machines, so it
helps me make sure I'm not issuing commands on some remote box I
admin.

-tim






More information about the Blinux-list mailing list