1

So, I am trying to run a Python script from within a Bash script. The Python script takes one of its parameters in the form of a JSON string, which looks like this:

'[{"CONFIG":"SomeConfigID"}]' //there are other members available but this is all I need.

I would like to pass in a variable from the Bash script as a member of the JSON object. So, This is what I am doing:

'[{"CONFIG":"${CONFIG}"}]'

However, what I find is that the Python script fails because it does not recognise ${CONFIG}

For some reason, Bash is not replacing that expression with the value of the variable. I am unfamiliar with Bash, so this may simply be something obvious that I am missing. Can anybody help? Is there some other way to construct this JSON string that would not cause this problem?

1
  • 1
    In Bash, a variable within single quotes is treated as literal. To have its value expanded you need to double quote it. Thus, you must probably swap the quotes you are using: does "[{'CONFIG':'${CONFIG}'}]" work to you? Commented Feb 25, 2015 at 10:22

1 Answer 1

4

Get the right quoting in place for shell expansion:

'[{"CONFIG":"'"${CONFIG}"'"}]'

As shell variable must not in single quote for shell to expand them.

Example:

CONFIG=foo
echo '[{"CONFIG":"'"${CONFIG}"'"}]'
[{"CONFIG":"foo"}]
Sign up to request clarification or add additional context in comments.

Comments

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.