I have a script containing two functions. one function starts the other as a background process and returns that processes UID. This works well, as long as I don't want to put the PID into a dedicated variable. Once I try to assign the PID to a variable, the script hangs until the background operation finishes.
This behaviour can be demonstrated by the following script:
#!/bin/sh
foo () {
bar & # start bar in background
echo $! # return bars' PID
}
bar () {
sleep 5 # some background operation
}
FOO=$(foo) # remember the PID of bar
echo $FOO # expectation: should echo immediately
# reality: echo after five seconds
So, my question is: How can I get the script to continue executing after starting the bar function, without waiting for bar to return?