Bash – History command inside bash script

bashcommand historyscripting

History is a shell-built in command I couldn't able to use that within a BASH script. So, Is there a way attain this using BASH script ?
Here we go my script for you:

#!/bin/bash
history |  tail -100 > /tmp/history.log
cd /tmp
uuencode history.log history.txt  | mail -s "History log of server" hello@hel.com

Best Answer

Bash disables history in noninteractive shells by default, but you can turn it on.

#!/bin/bash
HISTFILE=~/.bash_history
set -o history
history | tail …

But if you're trying to monitor activity on that server, the shell history is useless (it's trivial to run commands that don't show up in the history). See How can I log all process launches in Linux.

If you're debugging a script then shell history is not the best way to get useful information. A much better tool is the debug trace facility: put set -x near the top of the script. The trace is written to standard error.

Related Question