I’m trying to make a script to start a second counter. [but later I want to add minutes too] but so far, it just keeps echoing 0, 0, 0, 0, over and over. :\
#!/bin/bash
seconds=0;
count()
{
export seconds=$[seconds + 1]
sleep 1;
count
}
count&
N=$!
trap "kill $N; exit 0;" 2
while true; do
echo $seconds
sleep 1;
done
Ignacio’s answer explains that your subshell’s environment is not visible to your parent process.
One way to create slaves like this is co-processes (with
coprocinzshand newerbashor with special syntax inksh). Yourbashprobably doesn’t support this yet.Here’s a variation on your idea that uses signals to send the updates to the parent. I’ve retained your basic structure where it doesn’t conflict: