Take the 2-minute tour ×

I have recently switched on timestamps in my bash history. I am using three options to get more out of my history.

HISTTIMEFORMAT="[%F %T %Z] "; shopt -s histappend; PROMPT_COMMAND='history -a'

I cobbled together a bash script, rather than setting HISTSIZE to a ridiculously large number every time I want to look at history without having to brain-parse epoch time.

awk '/^#[0-9]+$/{ match($0,/[0-9]+/); 
    t = strftime("[%F %T %Z] ",substr($0,RSTART,RLENGTH)); 
    sub(/#[0-9]+/,t) } 1' ~/.bash_history | less

It's ugly, but it works. I imagine it could also be simplified, but that's only secondary to my reason for posting this. What I would like to do is prepend each command with its counterpart timestamp. I figured this can be done in sub(/#[0-9]+/,t), but when I try to match the \n, it does nothing at all. What must I do to have it recognize the newline?

Any pointers will be greatly appreciated.

share|improve this question
I for one don't understand what you're trying to do with \n. Just show us some sample input and expected output. –  Ed Morton Oct 5 '13 at 12:27
add comment

2 Answers

Depending on your environment you may need to escape either with quotes or an additional backslash.

\\n   or "\n"
share|improve this answer
add comment

The record separator \n is not part of the record $0.

You could modify your script slightly, so that it doesn't output the \n:

    sub(/#[0-9]+/,t); printf $0; next } 1' ~/.bash_history | less
share|improve this answer
add comment

Your Answer


