kislyuk / yq

Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
https://kislyuk.github.io/yq/
Apache License 2.0
2.62k stars 84 forks source link

Don't print 'null' to stdout for no-match #59

Closed stephen-dexda closed 5 years ago

stephen-dexda commented 5 years ago

For example, printing null to stdout breaks the following:

my_val="$(echo 'foo: bar' | yq -re '.someotherkey' || echo 'default_val')"

Since this will result in null instead of default_val.

Instead, yq should output an error message to stderr on no match, at least in the case of -e.

kislyuk commented 5 years ago

my_val="$(echo 'foo: bar' | yq -re '.someotherkey // "default_val"')" works.

The feature suggestion that you're making should be addressed to the upstream project that yq depends on, jq. In this case yq is just channeling the same output that jq is producing. The following discussion might also be informative: https://github.com/stedolan/jq/issues/354

tb3088 commented 5 years ago

FWIW the correct or at least readable way to do this in Bash is: my_val=$(jq --exit-status <some JQ expression>) || my_val="default_val"

I personally prefer the OR case to be unset my_val so later I can do things like: : ${my_val:=default_value}