$ /bin/sh -c "$(cat foo); foo"
, the $(cat foo)
part is evaluated in the outer sh
process, so the actual argument your sh
invocation is getting is:
$ /bin/sh -c "foo() {
printf "Hello World"
}; foo"
You have the function definition there and the call which works.
When calling
$ /bin/sh -c '$(cat foo); foo'
, the $(cat foo)
part is evaluated in your sh
process, so what's actually happening is $(cat foo)
is trying to interpret the first "command" from the foo
file, foo()
which is obviously not found
.