When does command substitution spawn more subshells than the same commands in isolation?
问题 Yesterday it was suggested to me that using command substitution in bash causes an unnecessary subshell to be spawned. The advice was specific to this use case: # Extra subshell spawned foo=$(command; echo $?) # No extra subshell command foo=$? As best I can figure this appears to be correct for this use case. However, a quick search trying to verify this leads to reams of confusing and contradictory advice. It seems popular wisdom says ALL usage of command substitution will spawn a subshell.