0

I want to substitute an echo command followed with | (pipe). For example,

$(echo "echo 'hello' | cat")

returns

'hello' | cat

I expect this to behave like

echo 'hello' | cat

which returns

hello

but that's not the case. Why?

PS. I'm aware of eval, and

eval $(echo "echo 'hello' | cat")

works as expected

1

Because syntax elements like | (or &&, or ; etc.) are recognized as the first thing of command line parsing, and are not processed again after parameters/variables are expanded.

Pretty much the only things that happen after parameter expansions, command substitutions and arithmetic expansions are word splitting and filename globbing. The outputs of expansions also don't expand again: this doesn't print 6:

$ var='$((1+2+3))'         # $((..)) not expanded here (single-quotes)
$ echo $var                # $((..)) not expanded here either
$((1+2+3))

Having another pass of parsing and expansions is exactly what eval is there for.

Related:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.