Bash signal capture not detecting variables changed after declaration of 'trap' block Bash signal capture not detecting variables changed after declaration of 'trap' block shell shell

Bash signal capture not detecting variables changed after declaration of 'trap' block


The trap is being interpolated, and is using the value of $done at the time the trap is being defined rather than when it is executed. You can use single quotes around the trap definition, or define a function. Defining a function is probably cleaner:

#!/bin/shdone=falsecleanup() { if test "$done" = true; then echo Test; fi; }trap cleanup EXITdone=true

This works because the expansion of variables in the function is deferred until the function is called, rather than when the function is defined.