Hacker News new | ask | show | jobs
by koeselitz 5752 days ago
Heh. This is funny timing, as .bash_history made me feel like an idiot just yesterday morning.

I was sitting here between queries and I thought 'y'know, all I ever do with history is grep for things. I wonder what else I can do?' When I want to learn about something quick, I usually start with the built-in help; so I did

history --help

The built-in help for history, unfortunately, is woefully inadequate; this was the output:

history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]

I said to myself: "Hmm. Wonder what those do?" -- and in a moment of bash stupidity, I thought: "well, let's try it!"

history -c

There's no output from that command, but it didn't take long to figure out what it does. It deletes your entire bash history, clearing the history in RAM and emptying .bash_history.

Since I use history all the time, I panicked. Holy crap! What have I done? I spent a few minutes internalizing the obvious lesson that everybody knows - never run commands without knowing them first. Thankfully, though, I had another bash open at the time. And after I'd beaten myself up over it, I thought for a moment, jumped over to the other shell, and did:

cd && history | cut -c 8- >> .bash_history

... because (after all) the evil, evil history -c command doesn't kill the history from RAM in other shells.

Lucky me.

2 comments

A heart surgeon once told me, surgery is not about how good you are at staying out of trouble, but how good you are at getting out of trouble. Eg, shit happens, the survivors are the ones who can deal with it.

*nix hacking ~ heart surgery? Who knew.

The built-in help for history, unfortunately, is woefully inadequate; this was the output:

i always wondered whether linux had such terrible man pages because nobody read them, or whether nobody read them because they were so terrible.

Well, since 'history' is a shell builtin, the thing to do would be 'help history', which actually gives a pretty thorough explanation of it. (And it's also explained pretty fully if you look at the actual man page for bash.) What the grandparent commenter saw was in fact just the brief "you invoked this command wrong" synopsis message, since '--help' isn't a flag recognized by the 'history' command (assuming his version of bash behaves similarly to mine).

Sure, there are some more obscure, less-commonly-used commands that are poorly documented, but for the basic stuff (and yes, that includes bash), I've found man pages to generally be a pretty good source of information.

Yes, the actual linux documentations are actually very good. I live on a server with hacked-together shell commands that are badly-documented, so I'm in the bad habit of doing the --help thing (the default around here).

The man pages are indeed very good. It's also worth noting that the Gnu people tend to prefer info pages - and 'history' is traditionally a Gnu-maintained command (as part of binutils) - so you'll get the absolute best documetation I know of (long explanations, interesting permutations, obscure hacks) if you do:

info history