yq — Basics
Key Access
yq uses jq-compatible syntax to navigate YAML structures.
The . operator is the identity — the entire document.
Dot-path notation descends into nested keys.
yq '.name' docs/antora.yml
# Output: captures
The bare .key expression returns the value at that key.
No flags needed for simple reads — yq defaults to YAML output.
yq '.name, .title, .version' docs/antora.yml
# Output:
# captures
# Work Chronicles
# null
Comma-separated expressions evaluate left to right.
null means the key exists but has no value (YAML ~).
Nested Paths
yq '.asciidoc.attributes.domain' docs/antora.yml
# Output: inside.domusdigitalis.dev
yq '.asciidoc.attributes["ise-ip"]' docs/antora.yml
# Output: 10.50.1.20
Bracket notation is required when keys contain hyphens.
attributes.ise-ip would be parsed as attributes.ise minus ip.
yq '.asciidoc.attributes["vault-port"]' docs/antora.yml
# Output: 8200
Keys and Length
yq 'keys' docs/antora.yml
# Output:
# - asciidoc
# - name
# - nav
# - start_page
# - title
# - version
yq '.asciidoc.attributes | keys | length' docs/antora.yml
# Output: ~134 (varies as attributes are added)
yq '.asciidoc.attributes | keys' docs/antora.yml | head -10
# Output:
# - ad-dc-hostname
# - ad-dc-ip
# - attribute-missing
# - author
# - bind-ip
# - bt-buds-mac
# - bt-buds-name
# - bt-kinesis-mac
# - bt-kinesis-name
# - category
Type Inspection
yq '.name | type' docs/antora.yml
# Output: !!str
yq '.nav | type' docs/antora.yml
# Output: !!seq
yq '.asciidoc.attributes | type' docs/antora.yml
# Output: !!map
!!str is string, !!seq is array/sequence, !!map is object/mapping.
These are YAML tags — yq preserves them, jq does not.
Array Traversal
yq '.nav[0]' docs/antora.yml
# Output: modules/ROOT/nav.adoc
yq '.nav[]' docs/antora.yml
# Output: modules/ROOT/nav.adoc
The [] operator iterates a sequence, producing one output per element.
[0] selects by index. Negative indices work: [-1] is the last element.
yq '.nav | length' docs/antora.yml
# Output: 1
Has and Existence
yq '.asciidoc.attributes | has("domain")' docs/antora.yml
# Output: true
yq '.asciidoc.attributes | has("nonexistent-key")' docs/antora.yml
# Output: false
has() returns a boolean. Use it in select() to filter collections.
Raw Output
yq -r '.asciidoc.attributes.domain' docs/antora.yml
# Output: inside.domusdigitalis.dev
Without -r, string values are quoted in the output.
With -r, the raw string is emitted — critical for shell variable capture:
domain=$(yq -r '.asciidoc.attributes.domain' docs/antora.yml)
echo "$domain"
# Output: inside.domusdigitalis.dev