Tricking bash HISTTIMEFORMAT
by Emile `iMil' Heitor - 2017-04-30
While trying to find a clean method to remove line numbers from the history
command, I found an interesting trick by using the HISTTIMEFORMAT
environment variable. Here’s what bash
’s man
says:
HISTTIMEFORMAT
If this variable is set and not null, its value is used as a
format string for strftime(3) to print the time stamp associated
with each history entry displayed by the history builtin. If
this variable is set, time stamps are written to the history
file so they may be preserved across shell sessions. This uses
the history comment character to distinguish timestamps from
other history lines.
But it turns out you can actually put pretty much anything in there, and for example, an ANSI
escape sequence that does a line feed and erases the current line:
$ HISTTIMEFORMAT="$(echo -e '\r\e[K')"
There we go, no more line numbers:
$ history |tail -1
history |tail -1