Why is the first line skipped if I remove echo "prevent remove first line"?
(echo "prevent remove first line" && find ~/somedir -mindepth 1 -maxdepth 1 -type d) | /usr/local/bin/jq -nR \
'{
"items": [
inputs |
inputs as $title |
{
"title": $title,
}
]
}'
>Solution :
Your example should be:
find ~/somedir -mindepth 1 -maxdepth 1 -type d | /usr/local/bin/jq -nR \
'{
"items": [
inputs as $title |
{
"title": $title,
}
]
}'
From the jq man page:
inputs
Outputs all remaining inputs, one by one.
This is primarily useful for reductions over a program´s inputs.
This implicitly means that every time called, inputs reads the next line from stdin. stdin is realized as a pipe, once a line from stdin has been read, it is gone.
When you echo a line before the actual input, then this extra line will be subject to this extra read. Just use inputs twice to see the problem coming back:
(echo "test" && find ~/somedir -mindepth 1 -maxdepth 1 -type d) | /usr/local/bin/jq -nR \
'{
"items": [
inputs | inputs |
inputs as $title |
{
"title": $title,
}
]
}'
PS: As pmf pointed out, the whole solution can be simplified to:
find ~/somedir -mindepth 1 -maxdepth 1 -type d \
jq -nR '{items: [{title: inputs}]}'