one thing to try in order to achieve a degree of asynchronous setting of a variable is:
#!/usr/bin/env bash
async() {
while :; do
# send SIGUSR1 to "parent" script
kill -USR1 "$1"
sleep 1
done
}
# provide PID of script to async
async $$ &
async_pid=$!
declare -i i=0
update() {
i=42
}
cleanup() {
kill ${async_pid}
}
trap update USR1
trap cleanup EXIT
echo $i
sleep 2
echo $i
One caveat with this approach is: if the script is sleeping and the signal is raised, the update command is only executed once when the script is "awake" again.