みつきんのメモ

組み込みエンジニアです。Interface誌で「Yocto Projectではじめる 組み込みLinux開発入門」連載中

bash-history-sqliteを使用する

GUI上でBashを使用していると、bash_historyが各端末ごとで共有できず、最後に閉じた端末の履歴のみが保存される。 このため、全てのBashを閉じてしまうと、最後に閉じた端末以外の履歴をさかのぼることができなくなる。

bash-history-sqliteを使用すると、GUI上で動作する全てBashの履歴がSqlite3の1つのDBに保存されるようになる。

ただし、hisotryコマンドを置き換えるものではなく、dbhistry(dbhsit)、dbexecというコマンドが使用できるようになる。

試しにalias history=dbhistとしてみたが、調子が悪い(履歴が保存されない)のでやめた。

依存パッケージのインストール

次のコマンドを使用するためインストールする。

sqlite3以外はすでに導入済みだと思われる。

$ sudo apt-get install -y wget sqlite3 gawk bash

導入方法

ダウンロード

$ git clone https://github.com/thenewwazoo/bash-history-sqlite.git
$ cd bash-history-sqlite

インストール

READMEには

Check out the repo, and add the contents of bash-profile.stub to the end of your ~/.bash_profile.

と書いてあるが、Ubuntu.bash_profileがなく、さらにbash-profile.stubの内容が正しくないため、次のようにする。

$ cat << 'EOF' >> ~/.bashrc

HISTDB="${HOME}/.hist.db"
source ~/.bash-history-sqlite.sh
EOF

次に

Place the bash-history-sqlite.sh script in your $HOME with filename .bash-history-sqlite.sh.

にしたがって、を下記を実行する。

$ mv ./bash-history-sqlite.sh ~/.bash-history-sqlite.sh

実行

履歴の確認

履歴の確認はdbhistory(dbhist)で行なう。

$ dbhist
       1    echo $HISTDB
       2    dbhist
       3    dbhist
       4    dbhist
       5    exit
       6    history
       7    emacs ~/.bashrc
       8    exit
       9    ls
      10    history

検索もできる。

$ dbhist git
     127    git status
     128    git log
     129    git branch
     130    git checkout -b debug
     131    git status

検索はsqlite3のLIKE句の構文を使用できる。

$ dbhist git%status
     108    git status
     119    git status
     125    git status
     127    git status
     131    git status
     138    git status

履歴の実行

過去のコマンドの実行(!123相当)はdbexecで行なう。

$ dbexec 123