Linux Fu: Supercharge Bash History

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.

18 thoughts on “Linux Fu: Supercharge Bash History

  1. If you don’t want your cli history on the Internet, you can always put this at the end of your $HOME/.bashrc:
    export PROMPT_COMMAND=”history -a; history -c; history -r; ${PROMPT_COMMAND};”

    Keep in mind it syncs whenever the prompt is redrawn, so if you run something in terminal A it won’t show up in terminal B’s history until the prompt redraws there. Basically means you need to hit Enter at an empty prompt if you want to refresh that terminal’s history.

    1. I use this to log the date, directory and command to ~/.bash_history_all on local system:

      export PROMPT_COMMAND=’echo $(date +%Y-%m-%dT%H:%M:%S) $(pwd): “$(history 1 | head -c 512 | sed “s/^[ ]*[0-9]\+[ ]*//” )” >> ~/.bash_history_all; echo -ne “\033]0;${PWD##*/}\007″‘

  2. Nope.

    Not uploading my bash history to a site that proudly says “We’re in early stage.” That sounds like there’s a long-term plan to monetize it. I suppose someone could figure out the comms protocol and build their own on-prem bashhub server if they really wanted to, but a vague promise to never access commands “unless required for support reasons” isn’t good enough for me.

    I wish you the best of luck, @rcaloras, but I’m not the sucker you’re looking for.

  3. More than satisfied with the current ‘local’ history feature of bash. But that is my use case. In my mind this is a solution to a non-existent problem :) .

    Not interested in ‘cloud’ solutions either. But that is another topic.

  4. Seems like it would be better to append the history (and all the context they mention) locally and then use something like syncthing to copy the history DB to the other places you want it to go. Only problem would be if you were ssh’ed into multiple boxes and typing commands faster than syncthing updated. It might have merge conflicts in that case.

  5. It feels irresponsible to me for Hackaday to promote a tool that uploads your shell history to some mystery server.

    I like the idea, but the implementation is disgusting.

  6. You can use spacebar before a command you don’t want in your history with this variables in your ~/.bashrc:
    export HISTCONTROL=ignoreboth
    export HISTSIZE=500

  7. There is also:
    –> https://github.com/ddworken/hishtory
    “It stores your shell history in context (what directory you ran the command in, whether it succeeded or failed, how long it took, etc). This is all stored locally and end-to-end encrypted for syncing to to all your other computers.”

    –> https://github.com/bashup/lore
    “It’s a simple tool that lets you use “lore files” (named .lore by default). If you’ve sourced lore in your shell, then any time you enter a subdirectory with a lore file in a parent directory, your existing history will be saved to your previous history file, and the lore file will become your new history file. As you enter commands, they’re saved immediately to the lore file, so even if your shell has to be hard-killed (or your computer gets rebooted), you won’t lose any history.”

  8. The article you mentioned discusses a tool called Bashhub, which is a history database for bash, zsh, and other shells. It offers features such as privately stored and encrypted command history across machines, command context information (directory, timestamp, system, return code), and the ability to search through your history using various criteria.

    One of the advantages of Bashhub is its ability to provide a centralized and searchable history across multiple shells and machines. This can be useful when you have several shell sessions open and want to access your command history from any of them. The tool allows you to search for specific commands based on directory, system, timestamp, or return code, providing convenient ways to retrieve past commands.

    The article mentions concerns about privacy and storing command history on a remote server. The developers claim that the data is encrypted and kept private, but it’s understandable to have reservations about storing sensitive information. Bashhub does not record passwords entered at prompts, but it’s worth considering the security implications of entering sensitive data on the command line in general.

    To address privacy concerns, Bashhub provides options to turn off recording or delete specific commands from the history. Additionally, you can apply filters to exclude certain commands from being recorded. However, it’s important to trust the developers and their implementation of security measures if you choose to use this tool.

    The article also speculates about the potential future cost of the service and the possibility of self-hosting the tool using the code available on GitHub. It’s worth considering these factors if long-term availability and cost are important to you.

    Ultimately, whether or not to use Bashhub depends on your comfort level with storing command history on a remote server and the convenience it provides in accessing and searching through your history across multiple machines. It’s advisable to carefully evaluate the security implications and make an informed decision based on your specific needs and preferences.

Leave a Reply to TimTCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.