How can ''mycmd'' and ''echo $(mycmd)'' have different output in zsh?

Viewed 21

Consider the following zsh script:

echo $(echo "yes")

Now, echo takes a string and turns it into standard output. On the other hand, the $(X) notation evaluates X and turns its standard output into a string.

Expectedly, the standard output is as follows:

 dteiml@DominiksMBP2019 ~ echo $(echo "yes")
yes

So far so good.

Now consider the following script involving bat. bat is a pretty-prettier for cat, kind of like Pygments.

echo "yes" | bat

The standard output is as follows:

 dteiml@DominiksMBP2019 ~ echo "yes" | bat
───────┬─────────────────────────────────
       │ STDIN
───────┼─────────────────────────────────
   1   │ yes
───────┴─────────────────────────────────

So I would expect the following command:

echo $(echo "yes" | bat)

to produce the same output (pretty-formatted). However, it just outputs "yes":

dteiml@DominiksMBP2019 ~ echo $(echo "yes" | bat)
yes

Based on the above, this is unexpected. Does it use some zsh api that would allow it to determine if it's being executed inside a $(X) block, or something else?

PS I don't really need this for anything, just trying to get a better understanding of bash/zsh.

Thanks in advance!

0 Answers
Related