I don't how to explain the output from the MRE below. I (believe I) understand that Bash is killing the left-hand Process 1 of the pipe line because the right-hand Process 2 has exited, and thus there is no one to read the standard out from Process 1.
What I don't understand is how the bash script that is Process 1 can detect that
What I don't understand is how the bash script that is Process 1 can detect that
jq has failed, but then report that the Result Code returned is 0.
Code:
$ cat foo.sh
#!/usr/bin/env bash
json='{
"members": [
"element 1",
"element 2",
"element 3"
]
}'
JQ="jq -Me" # M means Monochrome (no coloring), -e means fail on empty or null value retrieval
if ! $JQ '.["members"][]' <<< "${json}"
then
rc=$?
printf >&2 'jq failed to enumerate members[], rc="%s"\n' "$rc"
fi
$ ./foo.sh
"element 1"
"element 2"
"element 3"
$ ./foo.sh | false
jq failed to enumerate members[], rc="0"
$ ./foo.sh | cat -2
cat: illegal option -- 2
usage: cat [-belnstuv] [file ...]
jq failed to enumerate members[], rc="0"