Hi would like to ask if there is anyway to display .bash_history with timestamp using shell script?
i know that you should use history command with
HISTTIMEFORMAT="%d/%m/%y %T "
to display it in terminal but it does not work when i use it on shell script. It seem that you can't run history command on script. i can use cat ~/.bash_history on script but it does not contain the timestamp.
#!/usr/bin/env bash
# @(#) s1 Demonstrate bash history in script.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C
set -o history
pl " Results from history in script, first call."
echo " Hello, world from bash."
HISTTIMEFORMAT="%d/%m/%y %T "
history
pl " Results from history in script, second call."
echo " another Hello, world from bash."
history
exit 0
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
bash GNU bash 3.2.39
-----
Results from history in script, first call.
Hello, world from bash.
1 23/09/13 06:09:08 pl " Results from history in script, first call."
2 23/09/13 06:09:08 echo " Hello, world from bash."
3 23/09/13 06:09:08 HISTTIMEFORMAT="%d/%m/%y %T "
4 23/09/13 06:09:08 history
-----
Results from history in script, second call.
another Hello, world from bash.
1 23/09/13 06:09:08 pl " Results from history in script, first call."
2 23/09/13 06:09:08 echo " Hello, world from bash."
3 23/09/13 06:09:08 HISTTIMEFORMAT="%d/%m/%y %T "
4 23/09/13 06:09:08 history
5 23/09/13 06:09:08 pl " Results from history in script, second call."
6 23/09/13 06:09:08 echo " another Hello, world from bash."
7 23/09/13 06:09:08 history