0

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!

7
  • bat is checking whether its output is a TTY and declining to reformat for human-targeted output when the output is a FIFO instead of a TTY. This isn't a bash/zsh choice, it's a bat choice. Commented Sep 7, 2022 at 17:42
  • That said, if your shell were bash instead of zsh and bat did create the table in question (instead of doing that only if isatty(1) is true), you'd have some serious bugs unless you changed your code to echo "$(echo "yes" | bat)" with the quotes. Commented Sep 7, 2022 at 17:43
  • (Please only tag one or the other of bash and zsh, not both at the same time; they're not mutually compatible) Commented Sep 7, 2022 at 17:43
  • (this is the same as how ls --color=auto only writes color-formatting when going straight to a terminal, not when writing to a pipe). Commented Sep 7, 2022 at 17:45
  • ...for where bat performs this check, see github.com/sharkdp/bat/blob/…, using a variable populated at github.com/sharkdp/bat/blob/… -- atty::is(Stream::Stdout) Commented Sep 7, 2022 at 17:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.