Data Structures & Algorithms

How to Escape Quotes in YAML

This article will go through the escape quotes approach provided by YAML for the string data type. There are various methods for applying this concept in YAML. Since YAML is a data serialization language, we’ll use a docker compose YAML format file to implement the various methods for escaping quotes in a string variable. In this article, we will implement these examples using the Ubuntu command terminal.

Example 1: Escaping Single Quotes While Using Double Quotes

In this example, we escape the single quotes in a string variable while using the double quotes in the text. The YAML file is a docker compose file which is usually used for providing the details that a docker image requires for functioning. We use the Ubuntu command terminal to create a docker compose file in the YAML format.

We use the given commands as seen in the previous excerpt. The first command is the cd Desktop command which navigates us into the desktop directory. Then, in the desktop directory, the mkdir command is used to create a new directory for the docker image file. In this new directory, we use the nano command with the name of the file with the “.yml” extension. With this command, the terminal opens the YAML file in the same window. We can save the file in our directory and can later edit the file by using the text editor. When we open the directory folder from our desktop, we get the YAML file as shown in the following:


Now, we open this file on our text editor where we can edit and save the file as shown in the following excerpt:


The YAML file in the previous excerpt stores the multiple variables that represent the functional components of a docker image file like the version it will run on and the services it stored in it. The last variable in this file named “Escape” is a string variable and its text is enclosed in single quotes but one word in the sentence is double quoted which is allowed in the YAML format and stored as it is.

Example 2: Escaping Double Quotes While Using Double Quotes

We use the double quotes in a string variable while escaping them in the text in this example. The YAML file is a docker compose file which is often used to provide information that a docker image needs to function. We generate a docker compose file in the YAML format using the Ubuntu command terminal.


We utilize the instructions shown in the preceding excerpt. The first command, cd Desktop, takes us to the desktop directory. The mkdir command is then used in the desktop directory to create a new directory for the docker image file. In this new directory, we run the nano command with the filename prefixed with “.yml”. The terminal opens the YAML file in the same window with this command. We save the file in our directory and update it later with the text editor. When we access the directory folder from our desktop, we see the following YAML file:


Now, we open this file in our text editor, change some variables, and save it as seen in the following extract:


In the previous clip, the YAML file contains several variables that reflect the functional pillars of a Docker image file such as the version it operates on and the services it contains. The last variable in this file, “Escape”, is a string variable with the text surrounded in double quotes. While several words in the text are separately enclosed in double quotes to be highlighted, which is acceptable in the YAML format and kept as it is.

Example 3: Escaping Single Quotes While Using Single Quotes

We create a docker compose file in the YAML format using the Ubuntu command line. We utilize the single quotes in a string variable while escaping them in the text in this example. The YAML file is a docker compose file which is frequently used to supply the information that a docker image needs in order to run.

We follow the directions in the preceding snippet. The first command, cd Desktop, takes us to the desktop directory. In the desktop directory, the mkdir command is used to create a new directory for the docker image file. In this new directory, we run the nano command with the filename prefixed with “.yml”. With this command, the terminal opens the YAML file in the same window. We store the file in our directory and then edit it with the text editor later. When we open the directory folder from our desktop, we see the YAML file shown in the following:

We now open this file in our text editor, make some changes, and save it as seen in the following excerpt:


The YAML file in the preceding clip has various variables that indicate the functional pillars of a docker image file, such as the version on which it runs and the services it contains. The last variable in this file, “Escape”, is a string variable with the text wrapped in single quotes, with one word in the text individually enclosed in between single quotes to be highlighted, which is permissible in the YAML format and preserved as it is.

Conclusion

We explored the numerous techniques that the YAML provides for escaping quotes in a string variable in this article. Quotes are used in a string variable as an identifier for the data type and on the other hand in normal text, these quotes are used for highlighting a word or group of words. This causes a conundrum that YAML easily avoids by using the escape technique as it is a smart language. We escaped both single and double quotes by using the escape techniques that YAML allows in several examples which were implemented in the Ubuntu 20.04 environment.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.