I'm trying to run a script:

A=multichain-cli chain97 issue 1XRnkvTc1Ev3q8UnSyynu1Qb9ss1E3aJWZn2bQ '{"name":"Test_Asset","open":true}' 100 echo "$A" 

I'm trying to pass Test_Asset as a parameter:

B=$1 A=multichain-cli chain97 issue 1XRnkvTc1Ev3q8UnSyynu1Qb9ss1E3aJWZn2bQ '{"name":"$B","open":true}' 100 echo "$A" 

Asset is created with Name as $B. I want asset to be created with the Value of B, not $B literally.

  • Please edit your question and clarify. "Isn't working" is useless information. How does it fail? What are you expecting to happen and what actually happens? What is multichain-cli? Is $D a text file? We can't help you if you don't explain what you are trying to do.
    – terdon
    Jul 31, 2017 at 10:50
  • @terdon "doesn't work" is sufficient information, and what multichain-cli is not not really relevant.
    – Bex
    Jul 31, 2017 at 10:52
  • @rahul, yes - variables should always be double-quoted. but the problem here is that $ASSET is inside single-quotes: '{"name":$ASSET,"open":true}'
    – cas
    Jul 31, 2017 at 10:54
  • 1
    @Bex depends very much on whether the issue is simply that the variable isn't being expanded or if the issue has to do with whitespace in the input parameters or something completely different like trying to run a bash script using csh as an interpreter. All sorts of things could be going wrong and we try to avoid guessing games as much as possible.
    – terdon
    Jul 31, 2017 at 10:55
  • 1
    @user1917578 please edit your question to add extra details. Comments are hard to read, easy to miss, a pain to format and can be deleted without warning.
    – terdon
    Jul 31, 2017 at 11:45

3 Answers 3


Inside single quotes, nothing is expanded. So you need to get out of the single quotes. In any case, you'd want to double-quote your variable expansions unless you want split+glob to apply to them, so:

A=$(multichain-cli issue '{"name":'"$ASSET"',"open":true}' "$D")
#                         ''''''''  """"""  '''''''''''''
printf '%s\n' "$A"

(the '''', """"" to show which parts are single-quoted, which are double-quoted).

Most likely, you'd want the content of $ASSET to be within double quotes in the JSON code, so you'd need to include them (in the literal, that is within single quotes part):

A=$(multichain-cli issue '{"name":"'"$ASSET"'","open":true}' "$D")
#                         '''''''''  """"""  ''''''''''''''

Don't confuse the double-quotes that are used for shell quoting (here to prevent split+glob upon the expansion of $ASSET) and the double-quotes that are passed verbatim to multichain-cli.

You may need to consider the cases where $ASSET contains double quotes itself or other characters (like backslash, newline or other control characters) that are special in the JSON syntax. For non-ASCII character, you'll also need to make sure they are are encoded in the way expected by multichain-cli.

Alternatively, you can use double-quote shell quoting for everything like @Bex showed, though that means you'd need to escape the "s (but also $, \ and `) with backslash in the text passed to multichain-cli (and avoid using the ancient `...` syntax for command substitution as it does additional processing with backslash).

  • still takes it as {\"name\":testxyzi1,\"open\":true} Jul 31, 2017 at 11:32
  • @user1917578, what do you mean? With that syntax, the 3rd argument that multichain-cli receives is {"name":content-of-$ASSET,"open":true} (no backslash). If you want it to be {"name":"content-of-$ASSET","open":true}, you need to include those extra double-quotes, for instance using: A=$(multichain-cli issue '{"name":"'"$ASSET"'","open":true}' "$D"). You may also have to consider the cases where $ASSET contains double quotes or other characters special in the JSON syntax. Jul 31, 2017 at 11:42
  • Value of ASSET doesn't contain a double quote. the value is being passed to the script and it is plain text. Jul 31, 2017 at 12:09


A="$(multichain-cli issue "{\"name\":$ASSET,\"open\":true}" "$D")"
echo "$A"

Variable replacement is not supposed to happen within literal quotes (that is the single '). Use double-quotes, and escape a double quote you want in the output with a backslash \.

  • Value actually passed is:{\"name\":testxyzi1,\"open\":true} Jul 31, 2017 at 10:57
  • @user1917578 Is that not to your satisfaction? What does your script look like now?
    – Bex
    Jul 31, 2017 at 11:01
  • working script is: A=`multichain-cli issue '{"name":"test_asset","open":true}' $D` Anything other than this, command creates with wrong name Jul 31, 2017 at 11:21

Thanks for the response guys. As a solution, I am writing the command to a file and then executing that file. This works as below:

X=`echo "'{\"name\":\"$ASSET\",\"open\":true}'"`
A="multichain-cli chain97 issue $X 100"
echo "$A" >test.sh
B=`sh test.sh`
echo "$B"
  • 3
    This solution is unsafe and prone to timing-dependent errors. Do not use it.
    – Bex
    Jul 31, 2017 at 12:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .