Occasionally, I will run a command that has a lot of output. Occasionally, the last 30-40 lines of that output (a.k.a. the only part of the output I ever really see) is fine, but much further up, there was an error. I'd like to make it easier to notice that the command failed. To do so, I want the return code to be part of my prompt. So I took my $PS1:
[\D{%Y-%m-%d} \t] \[\e]0;\u@\h: \w\a\]\$
...and extended it to this:
[\D{%Y-%m-%d} \t] ${?/^0$/} \[\e]0;\u@\h: \w\a\]\$
This results in a prompt like this:
[2011-05-10 09:38:07] 0 soren@lenny:~$
However, I'd like to find a way to have it only include the exit code if it was non-0. How can I do that? Sure, I could use
$(echo \$? | sed -e blah)
but as lightweight as sed is, it's still quite a bit more heavy weight than bash's builtin stuff.