Double quotes around a value make YAML read everything inside them as text, including characters that would otherwise mean something, such as a colon followed by a space.
Every command and output on this page is real, run with yq 4.54.3 on macOS.
Syntax
key: "text"
"…": double quotes around the whole value.- Inside them, a colon and a space are plain text.
- A backslash starts an escape, such as
\nfor a new line.
See Double-Quoted Style (opens in a new tab) in the YAML 1.2.2 specification.
Prerequisites
- yq, to check the YAML. Install it from yq’s install instructions (opens in a new tab).
The Example
A greeting with a colon in it, in a file named greet.yml.
Step 1: Write the YAML
greeting: "Hello: welcome!"
Step 2: Read It with yq
yq '.greeting' greet.yml
Hello: welcome!
yq prints the text without the quotes.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| No quotes around a value with a colon and a space in it | yq stops: Error: bad file 'greet.yml': go-yaml load error in scanner at L1.C16: mapping values are not allowed in this context | Put the value in double quotes |
| A missing closing quote | yq stops: Error: bad file 'greet.yml': go-yaml load error in scanner (while scanning a quoted scalar) at L1.C11-L2.C1: found unexpected end of stream | Close the quote at the end of the value |
FAQ
How do I put a line break in a double-quoted string?
Write \n. With greeting: "Line one\nLine two", yq prints:
Line one
Line two
Conclusion
Put a value in double quotes when it contains characters YAML would otherwise read as syntax, such as a colon and a space, and close the quote at the end.
Additional Resources
- Double-Quoted Style (opens in a new tab) (YAML 1.2.2 specification)
- yq documentation (opens in a new tab)
0 comments