Click here to Skip to main content
15,918,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to pass a defined variable in a BASH CURL command. I can't seem to get the single quotes or double quotes correct and it results in an error:

line 22: --user: command not found


Hardcoding the value is no problem.

What I have tried:

My code example:

password=MyPS@!

curl "https://somewebsite.com" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--data "{\"header1\":\"text1\",\"header2\":\"text2\",\"header3\":\"text3\"}" \

#--user 'Administrator':'MyPS@!'  #< --- This works
--user 'Administrator':'$password'  #< --- Does not work
Posted
Updated 1-Nov-21 7:07am

1 solution

It is because of the uncontinued blank line and the single quotes around $password. The following works*:
Shell
curl "https://somewebsite.com" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--data "{\"header1\":\"text1\",\"header2\":\"text2\",\"header3\":\"text3\"}" \
--user 'Administrator':"$password"


*my test did not use curl, but echo, just to check the generated command.
 
Share this answer
 
Comments
wifinut 1-Nov-21 13:21pm    
👍Thanks so much! Now I learned that I cannot have a blank line in a CURL command.
(facepalm)
Richard MacCutchan 1-Nov-21 13:33pm    
The issue was that the blank line did not have a continuation character at the end, so the command string stopped there. The following line was then passed to the shell as if it was a command.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900