echo json over command line into file echo json over command line into file linux linux

echo json over command line into file


To preserve double quotes you need to surround your variable in single quotes, like so:

json='{"commit_id": "b8f2b8b", "environment": "test", "tags_at_commit": "sometags", "project": "someproject", "current_date": "09/10/2014", "version": "someversion"}'echo $json > versions.json

Take into account that this method will not display variables correctly, but instead print the literal $variable.

If you need to print variables, use the cat << EOF construct, which utilizes the Here Document redirection built into bash. See man bash and search for "here document" for more information.

Example:

commit="b8f2b8b"environment="test"...etccat << EOF > /versions.json{"commit_id": $commit, "environment": $environment, "tags_at_commit": $tags, "project": $project, "current_date": $date, "version": $version}EOF

If you're lookin for a more advanced json processing tool that works very well with bash, I'd recommend jq (http://stedolan.github.io/jq/manual/)