<!DOCTYPE html>
<html>
<head>
<title>Build a JSON string with Bash variables</title>
</head>
<body>
Build a JSON string with Bash variables
<p>
To build a JSON string with Bash variables, you can use the following steps:
</p>
<ol>
<li>
Create a string variable to store the JSON data.
</li>
<li>
Use the printf
command to format the JSON string.
</li>
<li>
Use the echo
command to print the JSON string.
</li>
</ol>
<p>
For example, the following code creates a JSON string representing a person:
</p>
<pre>
person_name="John Doe"
person_age=30
person_city="New York City"
json_string="{\"name\":\"$person_name\",\"age\":$person_age,\"city\":\"$person_city\"}"
echo $json_string
</pre>
<p>
This will output the following JSON string:
</p>
<pre>
{"name":"John Doe","age":30,"city":"New York City"}
</pre>
</body>
</html>