I’m in process of extending my knowledge of bash scripting. I came across following snippet:
!/bin/bash
# background-loop.sh
for i in 1 2 3 4 5 6 7 8 9 10 # First loop.
do
echo -n "$i "
done & # Run this loop in background.
# Will sometimes execute after second loop.
echo # This 'echo' sometimes will not display.
for i in 11 12 13 14 15 16 17 18 19 20 # Second loop.
do
echo -n "$i "
done
echo word # This 'echo' sometimes will not display.
It seems that this snippet output is non deterministic; and I would like to know why…
edit: so far; in 10 attempts “word” was always shown
edit2:
sample outputs:
11 1 12 13 14 2 15 3 16 4 17 5 18 6 19 7 20 8 9 word
1 11 12 13 2 14 3 15 4 16 5 17 6 18 7 19 8 20 9 10 word
11 12 13 14 1 15 16 17 2 18 3 19 4 20 5 6 word
Basically what is happening here is that you have 2 processes running at the same time. And as always in such case you cannot predict in which order they will execute. Every combination of output from first
forloop and secondforloop is possible.