Search code examples
jqexit-codeexitstatus

Exit with non-zero if input is empty


I am trying to parse my curl output with JQ and check the exit code, so when my curl output is empty, JQ return 0 exit code.

For example:

$ echo "" | jq '.key'
$ echo $?
0

How to make this return 1 or some non-zero value?


Solution

  • You should use input with -n/--null-input on the command line. This way JQ will fail if its input is empty.

    $ echo | jq -n 'input.key'
    jq: error (at <stdin>:1): break
    $ echo $?
    5
    
    $ jq -n '{key: 1}' | jq -n 'input.key'
    1
    $ echo $?
    0