I’m trying to get the redirect link from a site by using curl -I then grep to “location” and then sed out the location text so that I am left with the URL.
But this doesn’t work. It outputs the URL to screen and doesn’t put it
test=$(curl -I "http://www.redirectURL.com/" 2> /dev/null | grep "location" | sed -E 's/location:[ ]+//g')
echo "1..$test..2"
Which then outputs:
..2http://www.newURLfromRedirect.com/bla
What’s going on?
As @user353852 points out, you have a carriage return character in you output from
curlthat is only apparent when you try toechoany character after it. Thelesspager shows this up as ^MYou can use
sedto remove “control characters”, like in this example:Notes:
I used
awkrather than yourgrep [...] | sedapproach, saving one process.For me,
curlreturns the location in a line starting with ‘Location:’ (with a capital ‘L’), if your version is really reporting it with a lowercase ‘l’, then you may need to change the regular expression accordingly.