If you want to use the output of the history
command from an active shell session in a script, you can use an alias to run the command first. Then, in the same alias, you can call the remainder of the script. With such a configuration, you can achieve essentially the same result as having the history
command in the actual script.
For instance, you can create an alias like this, assuming the script's name is script.sh:
alias hy_tmp='history | tail -100 > /tmp/history.log ; bash /patch/to/script.sh'
And change the script to this:
#!/bin/bash
cd /tmp
uuencode history.log history.txt | mail -s "History log of server" [email protected]
I found this question while writing a process to combine, sort and synchronize ~/bash_history
files on two computers so it'll be easy to search commands I've used in the past.
It's much less of a hassle to update my cumulative history file without having to log into a new shell to have ~/bash_history
updated. For monitoring a server this will obviously not work, as mentioned in the other answers.
My usage in particular is:
alias hbye='history | cut -c 8- > /home/chris/.bash_history_c; bash /hby.sh
The script hby.sh
then pulls all unique entries from all ~/.bash_history*
files.