When calling $ /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:\n\n$ /bin/sh -c "foo() {\n printf "Hello World"\n}; foo"\n
\nYou have the function definition there and the call which works.\nWhen 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
.