Linux Fu: Supercharge Bash History [Hackaday]

View Article on Hackaday

Having a history of shell commands is a great idea. It is, of course, enormously handy when you have to run something repetitively or you make a simple mistake that needs correction. However, as I’ve mentioned in the past, bash history isn’t without its problems. For one thing, by default, you don’t get history in one window from typing in another window. If you use a terminal multiplexer or a GUI, you are very likely to have many shells open. You can make them share history, but that comes with its own baggage. If you think about it, we have super fast computers with tons of storage compared to the “old days,” yet shell history is pretty much the same as it has been for decades. But [Rcaloras] did think about it and created Bashhub, a history database for bash, zsh, and probably some other shells, too.

Command detail screen

You might think you don’t need anything more than what you have, and, of course, you don’t. However, Bashhub offers privately stored and encrypted history across machines. It also provides context about commands you’ve executed in the past. In other words, you can see the directory you were in, the exact time and date, the system you were on, and the last return code of the command.

They Call Me the Seeker

The best part, though, is you can easily search through your history using all that information. So if you want to know the last command you ran in the ~/.ssh directory of your computer named FISHMONGER (we don’t judge), then you can search for that.

You can also make searches interactive as you probably expect from history. In other words, you type, and it shows you things that match, giving you less and less as you type more.

Privacy, Please

Of course, some will be nervous about having history stored somewhere, and that’s a legitimate concern. The developers claim the data is encrypted and private, but it is stored on a server via https, not on your local machine. In addition, you can turn recording off and delete commands as you see fit. The history mechanism doesn’t record passwords you enter at a prompt and, honestly, if you are putting in sensitive data on the command line, you should probably think about the wisdom of that, anyway.

There is a way to put in a filter if there are things you really don’t ever want to put into history. Or add #ignore to the end of a command to keep it out of history. Of course, that all assumes you trust the developer to respect filters, store data encrypted, and all that.

What worries us more, however, is that it costs something to run that server. So we imagine eventually, they will want to charge for the service or it will go away. The code is on GitHub so, presumably, you could self-host it, if you were willing to work that out, but that seems like a lot of work.

Honestly,…

We like the idea. We’d have been just as happy, though, to have the history file located somewhere we could at least marginally control. Most people that would use this have some sort of publically-accessible server these days, and there are many cloud storage options.

However, if you don’t mind sending your history data over a presumably encrypted connection to a presumably encrypted database, maybe try it. It seems easy to uninstall. According to the documentation, you just remove ~/.bashhub. Of course, that leaves a little stub in your startup file that looks to see if that directory exists, but that’s harmless.

Installation is easy too:

curl -OL https://bashhub.com/setup && $SHELL setup

You can plug in the value of $SHELL manually if you like and use bash, zsh, or fish.

Examples

Want to find your last 10 emacs commands?

bh -n 10 "emacs"

Or make the search interactive (you can also integrate with fzf, if you like):

bh -i "emacs"

What are the last few commands you ran in /tmp?

bh -n 3 -d /tmp

It Works!

It works well and doesn’t seem to slow things down, although, with today’s computers and network connections, it is hard to tell. Running it on old hardware might be a different story. We can imagine some improvements like being able to find a line in history and turning it into an alias or saving it to a script. Statistic junkies might want to grab the raw data and answer questions like “What was my most used program on host computer DARKSTAR?”

We’ve talked about fzf before, and this works nicely with Bashhub. We wish we could store the database locally and sync it between our machines ourselves.