Site icon JD Bots

How to Write a String in Double Quotes in YAML

Featured card for How to Write a String in Double Quotes in YAML: a greet.yml file with the greeting Hello: welcome! in double quotes, a yq command that reads .greeting and prints it, and the notes The colon stays text and Close the quote.

How to Write a String in Double Quotes in YAML

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

Syntax

key: "text"

See Double-Quoted Style in the YAML 1.2.2 specification.

Prerequisites

The Example

A greeting with a colon in it, in a file named greet.yml.

Step 1: Write the YAML

greet.yml

greeting: "Hello: welcome!"

Step 2: Read It with yq

Terminal

yq '.greeting' greet.yml

Output

Hello: welcome!

yq prints the text without the quotes.

Common Mistakes

MistakeWhat happensDo this instead
No quotes around a value with a colon and a space in ityq stops: Error: bad file 'greet.yml': go-yaml load error in scanner at L1.C16: mapping values are not allowed in this contextPut the value in double quotes
A missing closing quoteyq 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 streamClose 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:

Output

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

Exit mobile version