Using the shell's history expansion for getting the commands executed in the shell session previous to running your script would not be doable unless the invoking shell saves its history to
$HISTFILE after each executed command, which
bash does not do by default, and exported the
HISTFILE variable, which it does not need to do.
By default, the
bash shell maintains an in-memory history for the current interactive shell session. This history is saved to
$HISTFILE when the shell session exits. When a new session that enables command line history starts, the saved history is read from that file (assuming that new shell uses the same
Your script, unless it has the
HISTFILE variable inherited from its invoking environment, will at most be able to access command line history of its own session, i.e. the commands in the script. If
HISTFILE is exported, but if the invoking shell never saved its history to
$HISTFILE before running the script, it would be impossible to get at the in-memory history of that parent shell session, and you would at most be able to access the historical history of sessions long since dead.