
How to determine which commands failed/succeeded in your Bash_History file? - bashit
I have a python application I use for taking notes in markdown. It automates several tasks for me and one of those tasks is parsing my command history and logging certain commands to a markdown file.<p>Parsing out the command and even the timestamp of each command (if your bashrc is setup correctly) in the Bash_History file is easy. However, what if one of these commands was typed wrong or perhaps I forgot a parameter or flag which caused the command to fail. There isn&#x27;t any bash&#x2F;zsh environment variable I can set to add this bit of information to the bash_history file like you would for including the timestamp.<p>How can I effectively and most easily solve this problem?<p>To be extra clear. Say I have a bash_history file with the following commands. How can I distinguish between failed&#x2F;succeeded commands?<p>---Bash_History File---<p>lsla<p>ls -la<p>cdd<p>cd<p>---END OF FILE---<p>For simplicity, lets say I want to parse this file and log all these commands that succeeded but ignore the commands that failed which are &lt;lsla&gt; and &lt;cdd&gt;. Obviously &lt;lsla&gt; is wrong as it&#x27;s missing the space and &#x27;-&#x27; character. The &lt;cdd&gt; is wrong cause (for sake of argument) lets say I fat fingered this command. That leaves two commands that succeeded. For a Bash_History file of 5,000 commands this becomes more complicated as each command could have several ways it could look (such as different params and options). So a lookup &quot;dictionary&quot; type of method wouldn&#x27;t work here.<p>Therefore, how can I easily determine which commands succeeded and which failed?<p>What method would best work across both bash&#x2F;zsh?<p>Are there environment variables I have to set? For example, to see the timestamp of each command I set the following variable in my bashrc<p>HISTTIMEFORMAT=&quot;%d&#x2F;%m&#x2F;%y %T &quot;<p>Do I need to write my own bash_history program for this? Would like to avoid that.
======
jolmg
> Do I need to write my own bash_history program for this? Would like to avoid
> that.

I didn't even know about HISTTIMEFORMAT, but seeing as how it puts the time in
a "comment", which I didn't even know was possible for these readline history
files, maybe there's some bash hook you can define that will have access to
the return code of each command run and append that to the history file with a
comment character on your own. This working depends on having configured bash
so that it writes to the history file incrementally (as soon as each command
is run and not until you exit bash).

If that fails, in your shoes, I would look into patching bash. I don't think
you'll find explicit support for what you want (you've already looked in the
documentation, right?), but I'd be happy to be proven wrong.

------
otras
You may be interested in this HN comment from roughly a year ago, in which a
user created a playful bash prompt script that displays the number of
successful commands run in a row:
[https://news.ycombinator.com/item?id=15563117](https://news.ycombinator.com/item?id=15563117).
Sample prompt:

    
    
      [0] /home/bar/g3 14:19 $ echo "boo"
      boo
    
      [1] /home/bar/g3 14:19 $
    

With this as an example ($LAST_STATUS in particular) it seems like writing a
custom bash_history program would be very possible.

