gotchaModeratepending
Gotcha: YAML multiline strings and unexpected parsing
Viewed 0 times
yaml multilineliteral blockfolded blockyaml booleanyaml gotcha
Error Messages
Problem
YAML multiline strings behave differently depending on syntax, causing unexpected trailing newlines, preserved indentation, or folded lines.
Solution
YAML multiline string types:
# 1. LITERAL BLOCK (|) - preserves newlines
script: |
echo hello
echo world
# Result: "echo hello\necho world\n"
# (Keeps newlines, adds trailing newline)
# 2. LITERAL BLOCK STRIP (|-) - no trailing newline
script: |-
echo hello
echo world
# Result: "echo hello\necho world"
# (Keeps newlines, NO trailing newline)
# 3. LITERAL BLOCK KEEP (|+) - keep all trailing newlines
script: |+
echo hello
echo world
# Result: "echo hello\necho world\n\n\n"
# 4. FOLDED BLOCK (>) - newlines become spaces
description: >
This is a long
description that will
be folded into one line.
# Result: "This is a long description that will be folded into one line.\n"
# 5. FOLDED BLOCK STRIP (>-)
description: >-
This is a long
description.
# Result: "This is a long description."
# (Folded, no trailing newline)
# COMMON GOTCHAS:
# Gotcha 1: Indentation matters!
data: |
line 1
indented line # This has 2 extra spaces!
line 3
# Gotcha 2: YAML booleans
# These are ALL booleans, not strings!
flag: yes # true
flag: no # false
flag: true # true
flag: on # true
flag: off # false
flag: "yes" # string "yes" (quote to force string)
# Gotcha 3: Numbers
version: 1.0 # float, not string!
version: "1.0" # string (quote it)
zip: 01onal # string
zip: 01234 # octal number in some parsers!
zip: "01234" # string (always quote zip codes)
# Gotcha 4: Colons in values
title: Thing: A Story # ERROR! Colon starts a mapping
title: "Thing: A Story" # CorrectWhy
YAML's implicit typing and multiple string syntaxes cause subtle bugs. The Norway problem (NO parsed as false) and colon-in-value errors are especially common.
Context
Configuration files in YAML format
Revisions (0)
No revisions yet.