2

my idea is to put json string into variable JSON, i have a command whom takes a console login to user in AWS IAM, the comand is : aws iam create-login-profile --cli-input-json file://create-login-profile.json

in create-login-profile.json is the json following content :

{
"UserName": "roberto.viquezzz", 
"Password": "aaaaaaaaaaa", 
"PasswordResetRequired": true}

i try to write bash script whom contains json var "JSON" like following code:

JSON="{\"UserName\": \"roberto.viquezzz\",\"Password\": \"aaaaaaaaaaa\",\"PasswordResetRequired\" : true}"
aws iam create-login-profile --cli-input-json $JSON

and if i type in console ./file.sh the file execute create a console user. if it try to execute this code i get an error : Unknown options: "aaaaaaaaaaa","PasswordResetRequired", :, true}, "roberto.viquezzz","Password":

but if i execute this code from command line like :

aws iam create-login-profile --cli-input-json "{\"UserName\": \"roberto.viquezzz\",\"Password\": \"aaaaaaaaaaa\",\"PasswordResetRequired\": true}"

all is be ok , maybe whom know whats wrong ? please suggest!

3
  • 1
    ALWAYS put double quotes around the use of a variable in bash. Otherwise all nasty characters in its value (spaces, newlines, etc.) are interpreted to split the string into several arguments. Commented Oct 11, 2017 at 12:15
  • You don't need double-quotes unless you have some variables to expand inside. Just use single-quotes jsonVar='{ "UserName": "roberto.viquezzz", ; "Password": "aaaaaaaaaaa", ; "PasswordResetRequired": true}'; echo "${jsonVar}" Commented Oct 11, 2017 at 12:17
  • @Inian he would still need to quote it. Unless he adds the single quotes inside the value of the variable. Commented Oct 11, 2017 at 12:20

1 Answer 1

4

Put quotes around $JSON:

aws iam create-login-profile --cli-input-json "$JSON"

The quotes that are there during the assignment get consumed by the shell. You can verify this by issuing echo $JSON. By adding the quotes you will make sure that the entire string is passed to the command "aws" as a single argument.

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.