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.
sleep 5 && var=value2
, but what is the usecase of this job? – αғsнιη May 02 '20 at 17:00$val
, and have the rest of the code be able to see those updated values? If so (and also if your intention is something else), this should be made clearer in the question. – Kusalananda May 02 '20 at 18:22