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

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


'hello' | cat

I expect this to behave like

echo 'hello' | cat

which returns


but that's not the case. Why?

PS. I'm aware of eval, and

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

works as expected


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

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


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.