0

Hello how can I read a json from filesystem with bash and pass the string as an argument to a python script? I tried this. But it does not work

config=`cat test.json | tr '\n' ' '`
python3 script.py\
    --config $config  \
2
  • 1
    Just quote the variable, which you should always do unless you have a good reason not to. Commented Oct 1, 2020 at 15:00
  • See stackoverflow.com/questions/29378566/… Commented Oct 1, 2020 at 15:04

1 Answer 1

2

Why eliminate the newlines?

python3 script.py --config "$(<test.json)"

Or if you really need them gone,

python3 script.py --config "$( tr "\n" ' ' < test.json)"
Sign up to request clarification or add additional context in comments.

3 Comments

The important thing is that you've quoted the command substitution.
Which retains all the whitespace &c, but should not be confused by similar quotes embedded in the data.
Or --config "$(jq -c . test.json)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.