0
getcode=$(curl http://www.fromsomewhere.com/?xxxxx=1) (from an API that returns a number value. eg 5555)

echo $getcode  #5555(able to get #5555)

curl "http://www.fromsomewhere.com/?qs=$getcode&qss=1&qqq=123"

I can't really do this as I found the url looks.

echo "http://www.fromsomewhere.com/?qs=$getcode&qs.condition=1&qqq=123"

the output is: &qs.condition=1&qqq=123e.com/?qs=555 It replaces the url http://......

What is the proper solution to have the url print properly like "http://www.fromsomewhere.com/?qs=5555&qs.condition=1&qqq=123"?

oddly if I only have

http://www.fromsomewhere.com/?qs=$getcode

the url looks fine.

however, when I add an & http://www.fromsomewhere.com/?qs=$getcode&qs.condition...

then it looks wrong again

olo
  • 5,225
  • 15
  • 52
  • 92
  • are you running this from a bash script or a shell? – naimdjon Dec 22 '20 at 09:21
  • @naimdjon I am running from bash script – olo Dec 22 '20 at 09:23
  • Could you paste the whole script? – naimdjon Dec 22 '20 at 09:24
  • I've tried this one, and it works as expected: ```getcode=$(curl http://localhost:8000/foo.txt?xxxxx=1) echo $getcode #5555 echo "http://www.fromsomewhere.com/?qs=$getcode&qss=1&qqq=123"``` – naimdjon Dec 22 '20 at 09:31
  • Thanks @naimdjon this is about almost the whole lot. `curl` to get a code from an API. e.g get `5555` then wanted to use the variable in another curl. Maybe something wrong from API returned value ... will double check – olo Dec 22 '20 at 09:32
  • Yes, I found static value is ok, and the `variable` inside a url replacing some strings. probably something to do with API not bash ... sorry – olo Dec 22 '20 at 09:33
  • So when you run getcode="#5555" and then run the curl command, everything words as expected? – Raman Sailopal Dec 22 '20 at 09:39
  • Yes! so odd.. @RamanSailopal – olo Dec 22 '20 at 09:42
  • I assume you get a `\r` in your first call: `#5555\r` – Marco Dec 22 '20 at 09:45
  • If you run getcode=$(curl http://..... and then echo "$getcode" (note the quotes), do you see anything different? – Raman Sailopal Dec 22 '20 at 09:46

1 Answers1

1

I am sure you get a \r at the end in your first curl call. See example:

$ printf -v getcode '5555\r'
$ echo "http://www.fromsomewhere.com/?qs=$getcode&qs.condition=1&qqq=123"
&qs.condition=1&qqq=123e.com/?qs=5555

exactly the output you have.

Marco
  • 824
  • 9
  • 13