6

I'm puzzled by this behaviour of cat when trying to output a heredoc containing JSON in bash 3.2:

input:

$ cat <(cat <<EOF
> {"x":[{"a":1,"b":2}]}
> EOF)

output:

{"x":["a":1]}
{"x":["b":2]}

What's going on?

  • Cannot reproduce. Which version of bash? – muru Jul 2 '15 at 17:37
  • I cannot reproduce this on Fedora 14 or 21 nor CentOS 6.6 what are you using? – Eric Renouf Jul 2 '15 at 17:38
  • Bash 3.2 - I will update the question – Armand Jul 3 '15 at 9:01
  • This "brace expansion" can be disabled with set +B. – Armand Jul 3 '15 at 9:06
10

This is just shell variable expansion by bash. In this context whatever is between the curly braces will be iterated and expanded into the expression.

$ echo var{1,2,3,4}
var1 var2 var3 var4

$ echo var{1..10}
var1 var2 var3 var4 var5 var6 var7 var8 var9 var10
1

Not an answer, but a formatted comment:

$ cat <(cat <<EOF
> {"x":[{"a":1,"b":2}]}
> EOF)
bash: warning: here-document at line 15 delimited by end-of-file (wanted `EOF')
bash: warning: here-document at line 15 delimited by end-of-file (wanted `EOF')
{"x":[{"a":1,"b":2}]}

Put the closing parenthesis on a new line

$ cat <(cat <<EOF
> {"x":[{"a":1,"b":2}]}
> EOF
> )
{"x":[{"a":1,"b":2}]}

and clearly, I can't reproduce:

$ echo $BASH_VERSION
4.3.39(1)-release
0

Brace expansion is subject to the "braceexpand" option. For example:

set +B
cat <(cat <<EOF
{"x":[{"a":1,"b":2}]}
EOF
)

produces the single JSON value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.