rjpj1998
rjpj1998

Reputation: 419

Variable expansion is not working as intended with curl

I am having some trouble figuring out the expansion of a variable in my shell script. If I replace the variable with the desired string it works.

#!/bin/zsh

KEY="$(curl -Ivs -X GET "http://admin:[email protected]" &> >(awk '/^> Authorization/{ print $3 " " $4 }'))" 
# The string returned by the curl and awk command is Basic "YWRtaW46YWRtaW4=" without double quotes.

curl -H "Authorization: $KEY" "http://192.168.1.1/userRpm/WlanMacFilterRpm.htm?Page=1&exclusive=1" 
# This doesn't work

curl -H "Authorization: Basic YWRtaW46YWRtaW4=" "http://192.168.1.1/userRpm/WlanMacFilterRpm.htm?Page=1&exclusive=1" 
# This works 

The only thing thats different in the above two lines is.

Upvotes: 1

Views: 279

Answers (1)

that other guy
that other guy

Reputation: 123510

HTTP generally uses DOS style CR+LF line terminators. Whenever you parse data out of curl, you have to account for this.

To check if this is the problem, run your script with bash -x yourscript or zsh -x yourscript to see trace output that shows otherwise invisible carriage returns:

var=$'value\C-M'   # zsh
var=$'value\r'     # bash

(Dash and ash/busybox unfortunately doesn't highlight this problem, so try with one of the above shells)

To strip them, pipe your data through tr -d '\r'.

Upvotes: 2

Related Questions