I'm trying to save value from a curl command to a variable in a bash script.

Script looks like this



HTTP_STATUS=$(curl -IL --silent $username:$password@$IP_addr | grep HTTP)
echo "echo the variable works!"
isOK=$(echo $HTTP_STATUS)
status="HTTP/1.1 401 Unauthorized"


if [[ $HTTP_STATUS == $status ]]; then
    echo "The same the same!"
    echo "$isOK is not the same as $status"


if [ "$status" == "$isOK" ]
    echo "The same the same!"
    echo "$isOK is not the same as $status"

I am deliberately passing the wrong password in order for curl to return HTTP/1.1 401 Unauthorized. I want a function to check if wrong credentials are sent to a server.

The strange thing is that when I save the output from the curl command ie

HTTP_STATUS=$(curl -IL --silent $username:$password@$IP_addr | grep HTTP | tee $curr/test.txt)

To a file with tee I get this printed in file HTTP/1.1 401 Unauthorized. But if I remove the tee command ie

HTTP_STATUS=$(curl -IL --silent $username:$password@$IP_addr | grep HTTP)

And execute the script I get following print in terminal

echo the variable works!
HTTP/1.1 401 Unauthorized

is not the same as HTTP/1.1 401 Unauthorized

is not the same as HTTP/1.1 401 Unauthorized

I tried with following as well but same result

HTTP_STATUS=`curl -IL --silent $username:$password@$IP_addr | grep HTTP`

The variable HTTP_STATUS seems to be blank when I do the checks in the if statements. How is this possible and why is the output of the command saved to a file with tee and echo the variable works but not when using the variable in the if statements?

Best regards

  • Always quote your parameter expansions until you learn when it is safe or appropriate to leave them unquoted.
    – chepner
    Dec 17, 2016 at 17:34
  • I found a answer here stackoverflow.com/questions/12444125/…. For some reason. When using stars ** around "$status" such as "$status" it works. But I still don't know why this is!
    – S4M1R
    Dec 17, 2016 at 17:55
  • The stars create a pattern, which matches any string that starts with a (possibly empty) sequence of characters, then $status, then ending with a (possibly empty) sequence of characters.
    – chepner
    Dec 17, 2016 at 18:00

1 Answer 1


The HTTP protocol requires that header lines end with <CR><LF> (carriage return and line feed, \r\n in UNIX notation). To see what curl actually returns you can try:

curl -IL --silent $username:$password@$IP_addr | grep HTTP | cat -v

In UNIX, <LF> terminates the text line, and <CR> is just an ordinary character with no special meaning. The apparently missing $isOK in the subsequent messages is due to the trailing <CR>, which moves the cursor back at the beginning of the line. In detail, the line

echo "$isOK is not the same as $status"

writes out

HTTP/1.1 401 Unauthorized<CR>
 is not the same as HTTP/1.1 401 Unauthorized

both on the same line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .