Using history command in bash script

Johnny Rosenberg gurus.knugum at gmail.com
Fri Feb 24 12:12:08 UTC 2012


Seems not to be possible.

~$ cat > pa
#!/bin/bash

history
history
history
<Ctrl+d>
~$ chmod +x pa
~$ pa
~$

So there seem to be no history entries available when I run history
from a bash script. Why is this?

I also tried to use the ~/.bash_history file, but it doesn't seem like
events are added to it for each command executed, since at least a
couple of ten commands are missing at the end. Seems like things are
added to the .bash_history file ”now and then”, I'm not sure how
often. How can I get around this? Where are the latest history lines
recorded before they are added to the actual .bash_history file?

I'm trying to make a bash script that creates an alias for my last
command and save it in my .bash-aliases file (which is launched from
the .bashrc file), so I need some way to know what my latest command
was.


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ




More information about the ubuntu-users mailing list