Sample Header Ad - 728x90

Will "$!" reliably return the correct ID with "&"?

0 votes
2 answers
51 views
In my tests, I always get the correct result so far with this:
[fabian@manjaro ~]$ sleep 10 & echo $!
 302657
302657
But sleep and echo are getting executed simultaneously here, so I would expect that it can sometimes happen that echo executes before the value of $! is set properly. Can this happen? Why doesn't it so far for me? My ultimate goal: Execute two tasks in parallel and then wait for both before moving on. The current plan is to use something like foo & bar; wait $!; baz. Will this always work or can it sometimes wait for an arbitrary older background process (or nothing at all, if $! is empty)?
Asked by Fabian Röling (369 rep)
Dec 13, 2020, 06:02 PM
Last activity: Dec 13, 2020, 09:51 PM