Skip to content

Instantly share code, notes, and snippets.

@jakub-g
Last active September 29, 2022 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakub-g/b2ef123f8b754bc5c63bc854e7019987 to your computer and use it in GitHub Desktop.
Save jakub-g/b2ef123f8b754bc5c63bc854e7019987 to your computer and use it in GitHub Desktop.
jq JSON with newlines output formatting madness - bash vs zsh discrepancy
$ jq --version
jq-1.6
---
bash-3.2$ jq --null-input --compact-output --raw-output --monochrome-output --arg test 'A\nB' '{test: $test}'
{"test":"A\\nB"}
bash-3.2$ OUT=$(jq --null-input --compact-output --raw-output --monochrome-output --arg test 'A\nB' '{test: $test}'); echo $OUT
{"test":"A\\nB"}
zsh-5.8.1> jq --null-input --compact-output --raw-output --monochrome-output --arg test 'A\nB' '{test: $test}'
{"test":"A\\nB"}
zsh-5.8.1> OUT=$(jq --null-input --compact-output --raw-output --monochrome-output --arg test 'A\nB' '{test: $test}'); echo $OUT
{"test":"A\nB"}
# Folks on SO suggest to use printf %b to obtain \n instead of \\n
# https://stackoverflow.com/questions/67391180/use-newline-with-jq
# But discrepancy in zsh between the modes are still there
zsh-5.8.1> jq --null-input --compact-output --raw-output --monochrome-output --arg test "$(printf %b 'A\nB')" '{test: $test}'
{"test":"A\nB"}
zsh-5.8.1> OUT=$(jq --null-input --compact-output --raw-output --monochrome-output --arg test "$(printf %b 'A\nB')" '{test: $test}'); echo $OUT
{"test":"A
B"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment