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't any bash/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/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 <lsla> and <cdd>. Obviously <lsla> is wrong as it's missing the space and '-' character. The <cdd> 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 "dictionary" type of method wouldn'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/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="%d/%m/%y %T "<p>Do I need to write my own bash_history program for this? Would like to avoid that.
> Do I need to write my own bash_history program for this? Would like to avoid that.<p>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).<p>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.
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: <a href="https://news.ycombinator.com/item?id=15563117" rel="nofollow">https://news.ycombinator.com/item?id=15563117</a>. Sample prompt:<p><pre><code> [0] /home/bar/g3 14:19 $ echo "boo"
boo
[1] /home/bar/g3 14:19 $
</code></pre>
With this as an example ($LAST_STATUS in particular) it seems like writing a custom bash_history program would be very possible.