Bash command substitution ( $(...) ) forcing process to foreground
If you want the backgrounded process to not interfere with command substitution, you have to disconnect its stdout. This will return immediately:
$ cat bg.sh #!/bin/shecho beforesleep 5 >/dev/null &echo after$ date; x=$(./bg.sh); date; echo "$x"Sat Jun 1 13:02:26 EDT 2013Sat Jun 1 13:02:26 EDT 2013beforeafter
You will lose the ability to capture the backgrounded process's stdout, but if you're running it in the background you probably don't care. the bg.sh
process can always write to disk.