For bash v4+ :
Use interactive mode in shell.
Set she-bang as
history inside of your script will work.
$ cat test.sh
history | wc -l
$ sed -i '1s/bash/bash -i/' test.sh
Commands was executed inside of script are not recorded into history.
For bash v3 (and possible for older ones)
The way above doesn't work for this vesions of bash. However you can remove she-bang at all and history will work well. This way also works great for bash v4.
set she-bang as interactive and do not forget about
set -o history like chepner mentioned.
set -o history
history |tail -n2 |head -n1 doesn't equal to the last command. it is command before last one.
Note if last or prelast commands were multiline it won't return the correct result.
btw in console you can use
!-2 to refer to prelast command instead of your strange construction. unfortunately it seems doesn't work in shell script even in interactive mode.