LoginSignup
9
3

More than 5 years have passed since last update.

history コマンドをシェルスクリプト内で使う方法

Last updated at Posted at 2018-05-24

Bash の場合、スクリプト内での history コマンドはデフォルトで無効化されています。

history コマンドを有効にするためには、
以下のように -o history オプションをセットします。

#!/bin/bash
set -o history  # historyを有効化
export HISTTIMEFORMAT='%F %T '  # historyの出力に時刻情報を追加

echo test

history

上記を実行すると、

 1  2018-04-29 11:58:47 export HISTTIMEFORMAT='%F %T '
 2  2018-04-29 11:58:47 echo test
 3  2018-04-29 11:58:47 history

という風にスクリプト内で実行したコマンドの履歴が出力されます。

スクリプト内での実行履歴ではなく、
コマンドプロンプトに打った履歴がほしい場合は、

history -r /path/to/.bash_history

のようにして履歴ファイルを読み込むこともできます。
(ファイルの場所はシェル変数のHISTFILEで指定されています)

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3