3 ms·
If I really had to get some JSON into a Bash script, and I couldn't just stick it in a file, I'd probably use a "heredoc" something like this: $ my_json="$
by oconnor663 5y ago
If I really had to get some JSON into a Bash script, and I couldn't just stick it in a file, I'd probably use a "heredoc" something like this:
$ my_json="$(cat <<EOF
> {
> "foo": "bar",
> "baz": 42
> }
> EOF
> )"
$ echo "$my_json"
{
"foo": "bar",
"baz": 42
}
But I definitely didn't remember how to handle the closing paren and closing quote correctly, and I had to google for an example just now. So I'm not allowed to say this is easy to remember :)
- aidenn0 5y agoRandom bit of trivia: on ksh you can do foo="$(cat <<EOF)" whatever EOF This is left undefined in the POSIX standard and bourne shells don't allow it.