If I understood well, not realy.
Your question boil down to : am I running foobar
from "main" shell or echo $(foobar)
(command substitution) ?
foobar
may test the following
tty
(am I under a tty ? )
$SHLVL
(how many stack of shell ? )
Depending on your need, you may store and compare SHLVL
var, this supposed you (your shell) are the one that run the command.
I put quote arroud main, because there is no such thing as main shell, you have loggin shell (with a tty), shell forked from cron, daemons (mails, apache), but no "main" shell.
edit:
as pointed SHLVL might be tricky to use
archemar@home> cat t2.sh
echo "$SHLVL" $1
archemar@home> . ./t2.sh $SHLVL
3 3
archemar@home> ./t2.sh $SHLVL
4 3
archemar@home> ./t2.sh $(./t2.sh)
4 4
archemar@home>
$(foo)
foo | bar
? between$(foo)
and<(foo)
? etc. – Gilles 'SO- stop being evil' Mar 29 '16 at 22:12$(foo)
. Nothing else. – dgo.a Mar 30 '16 at 07:34