History With Time/Date?

Happy New Year to All! Wondering if there is a way to display time/date of last use of a command in my history file? ( i.e. .bash_history ). history --help gives:

…“If the HISTTIMEFORMAT 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 displayed history entry. No time stamps are printed otherwise.”

Not sure (tried to pipe history | grep date, etc.; no luck!) if the second line referencing “…format string…to print the time stamp…” is telling me what I want is available or no?
Regular use of history command of course gives 1. (number of command) <tab> <Command>. I’m looking to add time/date <Command> was last used. Any ideas?

Thanks, as always. Again: A Healthy, Safe & Prosperous New Year to all!!
— rob

Hi
You need to set that variable;


HISTTIMEFORMAT="%d/%m/%y %T "

echo $HISTTIMEFORMAT
%d/%m/%y %T

history

To make it permanent, you need to export it in your ~/.profile or ~/.bash_profile or set for systemwide… (too many ways…)

PS: for all the formats see ‘man 3 strftime’ :wink:

Thanks @malcolm for quick guidance, as always! I’ll be a couple of days chewing on this. . .concepts & terms from ‘Linux Programmer’s Manual’
Whew. . .like speaking Klingon to me. But will stick with it. Give you a shout by end of week. Thanks!

      • rob

Thanks for this thread. I’ve long wondered what was the secret to utilizing the numbers in .bash_history, but never came up with useful search terms when I looked.

‘which history’ produces not found, as does ‘cnf history’, yet ‘history --help’ works. How does the history command get run?

history is a shell builtin command and not a program.

Hi,

Since history is builtin to the shell, you can use

help history

HISTTIMEFORMAT has a default value It is probably set when bash is package/compiled from the distro. (assuming bash is your login shell which the default for openSUSE)

grep -i histtimeformat /etc/bash.bashrc

Afaik which is builtin from other shell but not bash and since which is external command from the shell, It has no knowledge of builtin commands.

a good example is cd, try looking for cd (using bash as your login shell) using which

which cd

It will not output anything, unless of course you have an executable in you PATH that has the same name.

I’d go with type since it is also builtin from the (bash) shell

type cd
type history

Builtins are commands that came from the shell
External commands/executables are located within you PATH which is the value of

echo "$PATH"

To show all the builtins you can use enable

help enable

Or just run

enable

See more about builtins, run

PAGER='less +/^SHELL\ BUILTIN\ COMMANDS' man bash

Also about HISTTIMEFORMAT, run

PAGER='less +/^:space:]]*HISTTIMEFORMAT' man bash

 > which -a which
/usr/bin/which
 > 
 > file /usr/bin/which
/usr/bin/which: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=5e13344a37016d40cd3e7b18c4e50a2f7d3a5e66, stripped
 > 

which” is simply an executable system file. :slight_smile:

That’s because you’re using bash, like I said "Other shell" and bash is not the only shell out there…

Hi
With a !<some_number_from_history> it will re-run the command?

Hi,

You can Take a look at the builtin command **fc **

help fc

and probably

man fc

I have never used it so that’s all what I can say about that builtin. :wink:

There’s a Bash alternative to “fc” – Readline line editing, which is enabled by default – with an Emacs style command interface – change it to a Vi style command interface by adding “set -o vi” to “~/.bashrc” …

Here, “which history” returns

history: shell built-in command.

But that’s because I am using “csh” (really “tcsh”) as my user shell.

And “which which” returns:

which: shell built-in command.

However:


/usr/bin/which history

tells me that “history” does not exist in any directory in my path.