Is it possible to use Bash history expansion inside the trap command?

59 Views Asked by At

Is it possible to use history expansion within a trap [commands] ERR?
I've tried manually enabling history expansion within the environment and within the trap commands argument (trap 'set -H; echo !-2 failed' ERR) but could not get it to work as the shell always ended up just interpreting "!-2" literally.

Every designator I've tried from Bash's manual ended up being interpreted literally once the trap got activated but they work just fine when called somewhere else in the same environment.

1

There are 1 best solutions below

0
KamilCuk On

!-2? Use:

history | tail -n2 | head -n1

or just:

history -p '!-2'