#bash has no threads: & starts a command or a function
#in the background as a separate process
add() {
echo $(($1 + $2)) > "result$3.txt"
}
#two jobs run aside at the same time, the script goes on;
#each job writes its own file, so the order of their work
#does not matter
add 3 5 1 &
add 10 20 2 &
#a job is a process with its own copy of the variables:
#what it changes does not come back to the script
counter=1
counter=2 &
#without wait the script could end before its jobs (topic 166)
wait
result1=$(< result1.txt)
result2=$(< result2.txt)
rm result1.txt result2.txt
#result1 is 8
#result2 is 30
#counter is 1
echo "result1 is $result1"
echo "result2 is $result2"
echo "counter is $counter"