Compress heredoc declaration to one line in bash?

I have this which works to declare a JSON string in a bash script:

   local my_var="foobar"
   local json=`cat <<EOF

The above heredoc works, but I can't seem to format it any other way, it literally has to look exactly like that lol.

Is there any way to get the command to be on one line, something like this:

   local json=`cat <<EOF{"quicklock":"${my_var}"}EOF`

that would be so much nicer, but doesn't seem to take, obviously simply because that's not how EOF works I guess lol.

I am looking for a shorthand way to declare JSON in a file that:

  1. Does not require a ton of escape chars.
  2. That allows for dynamic interpolation of variables.

Note: The actual JSON I want to use has multiple dynamic variables with many key/value pairs. Please extrapolate.


  • why not use jq? It's pretty good at managing string interpolation and it lints your structure.

    $ echo '{}' >> foo.json
    $ declare myvar="assigned-var"
    $ jq --arg ql "$myvar" '.quicklock=$ql' foo.json

    the text that comes out on the other end of that call to jq can then be cat into a file or whatever you wanna do. text would look something like this:

    {"quicklock": "assigned-var"}