Alternative (Default value)
This operator is used to provide alternative (or default) values when a particular expression is either null or false.
LHS is defined
Given a sample.yml file of:
a: bridgethen
yq '.a // "hello"' sample.ymlwill output
bridgeLHS is not defined
Given a sample.yml file of:
{}then
yq '.a // "hello"' sample.ymlwill output
helloLHS is null
Given a sample.yml file of:
a: ~then
yq '.a // "hello"' sample.ymlwill output
helloLHS is false
Given a sample.yml file of:
a: falsethen
yq '.a // "hello"' sample.ymlwill output
helloRHS is an expression
Given a sample.yml file of:
a: false
b: catthen
yq '.a // .b' sample.ymlwill output
catUpdate or create - entity exists
This initialises a if it's not present
Given a sample.yml file of:
a: 1then
yq '(.a // (.a = 0)) += 1' sample.ymlwill output
a: 2Update or create - entity does not exist
This initialises a if it's not present
Given a sample.yml file of:
b: camelthen
yq '(.a // (.a = 0)) += 1' sample.ymlwill output
b: camel
a: 1Last updated
Was this helpful?