Closed stephen-dexda closed 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
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}
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.