5

Based on this question: Docker Compose variable Substitution with Quotes vs without

what, if any, is the difference between quoted and unquoted strings in yaml?

This question mainly comes from my question on variable substitution in docker compose file -which is yml.

The official example given is:

db:
  image: "postgres:${POSTGRES_VERSION}"

I was wondering if this would be valid without strings:

db:
  image: postgres:${POSTGRES_VERSION}

If not, then why? What are the cases where I should be adding quotes when using variable substitution in docker? How do I recognise them? What is it called -in the case I need to do further research on them in google?

  • Possible duplicate of [Do I need quotes for strings in Yaml?](https://stackoverflow.com/questions/19109912/do-i-need-quotes-for-strings-in-yaml) – LukeFilewalker May 11 '18 at 20:42
  • I'll refer you to my articles about quoting strings in YAML http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html and YAML Schemas http://blogs.perl.org/users/tinita/2018/01/introduction-to-yaml-schemas-and-tags.html – tinita May 11 '18 at 20:44
  • For writing an answer, the question is too vague, IMHO. Since YAML is quite powerful, it does not only depend on the YAML version and Schema in use, but also on the processor/application and how they process the content – tinita May 11 '18 at 20:47
  • @tinita I update my question. –  May 11 '18 at 20:57

1 Answers1

3

The short answer is, there is no difference in most cases.

---
foo: bar

is the same as

---
'foo': 'bar'

You must use quotes when using special or reserved characters (like & or { on the beginning, for example, or :<space>).

You also must use quotes if the scalar would resolve as a special type, like true, false, null, numbers etc. This depends on the YAML version and Schema.

It also depends on the application/processor you are using. They might do different things depending on if the scalar is quoted or not.

In the example you gave, quoting is not necessary, if the processor is implementing YAML correctly.

In my articles you can read everything about quoting and Schemas/Types in YAML: http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html http://blogs.perl.org/users/tinita/2018/01/introduction-to-yaml-schemas-and-tags.html

Further explanation: YAML consists of nodes that have three basic data types: Scalars, Mappings and Sequences. A Sequence is a list of scalars, mappings or sequences. A Mapping is a map (or dictionary, hash) of keys to values, both of which can be of all three types. So you get a tree structure, or more correctly, a graph. A Scalar can be a string, a number or null, for example.

tinita
  • 3,987
  • 1
  • 21
  • 23