If I create a file containing
and run it, I get no output. Running it manually in a terminal or
sourceing the script file does produce output.
history not work from a file?
The second command does "work", but the history is not enabled for non-interactive shells which is why it returns nothing in your script.
Testing the default in a non-interactive shell:
#!/bin/bash set -o | grep history history
$ ./nohistory.sh history off
Enabling history with
set -o history:
#!/bin/bash set -o history set -o | grep history history
$ ./history.sh history on 1 set -o | grep history 2 history