Bash::History::Read - Utility to read bash history file entries


This document describes version 0.04 of Bash::History::Read (from Perl distribution Bash-History-Read), released on 2015-11-07.


From script:

 use Bash::History::Read qw(parse_bash_history_file);

 my $res = parse_bash_history_file("$ENV{HOME}/.bash_history");

Sample result:

   [undef, "some-command\n"],
   [1446715184, "du -sm\n"],
   [1446715190, "ls -l\n"],

From the command-line:

 % perl -MBash::History::Read -i.bak -e'each_hist {
       $PRINT = 0 if $TS < time()-2*30*86400; # delete old entries
       $PRINT = 0 if /foo/; # delete unwanted lines (e.g. matching some regex)
       s/(mysql\s+-p)(\S+)/$1******/; # redact sensitive information
   }' ~/.bash_history


This module provides utility routines to read entries from bash history file (by default ~/.bash_history). The format of the history file is dead simple: one line per entry, but when HISTTIMEFORMAT environment is set, bash will print a timestamp line before each entry, e.g.:

 ls -al
 less myfile

See each_hist for one routine to let you handle this format conveniently.


parse_bash_history_file([ $path ]) => array

Parse entries from bash history file. If unspecified, $path will default to HISTFILE environment variable or $HOME/.bash_history.

Return an array of entries, where each entry is <[$timestamp, $line]> and $timestamp can be undef if entry does not have a timestamp.

each_hist { PERL_CODE }

Will read lines from the diamond operator (<>) and call Perl code for each history entry. Can handle timestamp lines. This routine is exported by default and is meant to be used from one-liners.

Inside the Perl code, $_ is locally set to the entry content, $TS is locally set to the timestamp (and changes to this variable is ignored, except when you undefine the variable, which will remove the timestamp from output), $PRINT is locally set to 1. If $PRINT is still true by the time the Perl code ends, the entry (along with its timestamp) will be printed. So to remove a line, you can set $PRINT to 0 in your code. To modify content, modify the $_ variable.


Please visit the project's homepage at


Source repository is at


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


perlancar <>


This software is copyright (c) 2015 by

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.