Method 01: Multiline string using single quotes in a YAML file in Ubuntu 20.04
In this method, we will be using single quotes in a YAML file to create a multiline string. The String variable will be utilized to keep a passage with more than one line. For this method, we will be creating a docker compose file inside a directory which will be a support file for the Docker image file.
The directory will be created on our Ubuntu system using the following commands:
The desktop directory will be accessed through the cd command. Then, in the desktop directory, we will create a subdirectory where we will create a docker compose file with the YAML format.
After this, we will move into the sub directory by using the cd command. Then, with the nano command for docker compose with a “.yml” extension, we will create a YAML file in the subdirectory as we can see in the output below of the directory on our desktop:
The file would directly be opened on our terminal for editing. But we can also edit the file in the Ubuntu Text editor as we can see in the output below:
In the above snippet of text editor, we can see a compose file for a docker image. There are several variables in this file. In the variable named “Example”, we have added a string comprising multiple lines using the single quote character. This will allow the string variable to be written in more than one line. The string will be presented as a paragraph due to the single quotes even if the conventional way only allows one line string.
Method 02: Multiline string using double quotes in a YAML file in Ubuntu 20.04
A text with more than one line will be kept inside the String variable in this approach. We will build a multiline string in a YAML file by utilizing double quotes. For this strategy, we will create a docker compose file within a directory that will serve as a support file for the Docker image file.
The following commands will be used to create the directory on our Ubuntu system:
The desktop directory will be accessible using the cd command. Within it, we will construct a subfolder in which we will build a docker compose file in YAML format.
After that, we’ll use the cd command to navigate to the subdirectory. Then, use the nano command for docker compose with a “.yml” extension to generate a YAML file in the subdirectory, as seen in the output of the subdirectory on our desktop below:
The file will be opened in our terminal for editing. But we could also modify it in the Ubuntu Text editor, as shown in the output above. A composed file for a Docker image may be seen in the above text editor excerpt. There are various variables in this file. We have added a string consisting of multiple lines using the double quotation character to the variable labeled “Example.” This allows the string variable to be written across several lines. Because of the single quotes, the string will be shown as a paragraph even though the standard method only allows for a one-line text.
Method 03: Multiline string using double spaces in a YAML file in Ubuntu 20.04
For this method, we will create a docker compose file within a directory that will serve as a support file for the Docker image file. In this approach, we will use double spaces in a YAML file to construct a multiline string. A paragraph with more than one line will be kept inside the string variable.
On our Ubuntu system, the following commands will be used to create the directory:
We will navigate to the desktop directory using the cd command. Within it, we will create a subfolder in which we will create a docker compose file in YAML format.
Following that, we’ll go to the subdirectory using the cd command. Then, use the nano command for docker compose with a “.yml” extension to build a YAML file in the subfolder, as seen in the output of the subdirectory on our desktop below:
The file will be opened for editing within our terminal. But we can also edit it in the Ubuntu Text editor, as demonstrated in the result below.
A composed file for a Docker image may be seen in the above text editor excerpt. There are various variables in this file. We have added a string consisting of multiple lines using the double spacing technique to the variable labeled “Example.” This allows the string variable to be written across several lines. Because of the single quotes, the string will be shown as a paragraph even though the standard method only allows for a one-line text.
Conclusion
In this article, we discussed the defining methods of a string variable in a YAML file. The main focus of this article was about multiline strings that can act as a paragraph which is not allowed in YAML conventionally. So, we used different methods to assign multiline text to a string variable. The use of single and double quotation marks is usually adopted in this scenario where the text is written in multiple lines in between these characters. The use of double spaces in accordance with the indentation block was also used in a method to store multiple lines in a string variable.