1

When I run

youtube-dl -j "$youtube_url" | jq

I get a nice json output. However, when I run

json=$(youtube-dl -j "$youtube_url" | jq)

then jq prints it's usage page and after that youtube-dl fails with a broken pipe error. The json-variable is empty.

I'm guessing that jq runs before youtube-dl and since there is no data on stdin then it terminates printing the usage instructions. When jq is no longer running then there is no pipe to write to so then youtube-dl fails. This is just a guess though. I do not really understand what is going on here. Please enlighten me!

fuumind
  • 443

1 Answers1

1

Answered on Stack Overflow:

Just calling jq without a filter will throw errors if stdout isn't a terminal. […] Try jq '.'. […] Note that the filter is not really optional.

In your case:

json=$(youtube-dl -j "$youtube_url" | jq '.')

Side note:

In general when you run foo | bar, well written foo will wait if bar doesn't read fast enough; and well written bar will wait if foo doesn't write fast enough. It doesn't matter which one starts first.

We can argue if jq is "well written". Still your problem originated from its quirky behavior depending on stdout, not on stdin. Your hypothesis is false.