Output of cURL is corrupted when stored to a bash variable

This is what console outputs when I execute cURL directly from the terminal:

/# curl -ksi http://localhost/ 
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.6.2
Date: Sun, 14 Jan 2018 11:49:38 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive
Location: http://localhost/welcome/default
Cache-Control: private, max-age=0

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>

But if I try to save the output of the cURL to a variable the content of it ends up looking corrupted:

/# VAR=`curl -ksi http://localhost/`
/# echo $VAR
 </html>nter>nginx/1.6.2</center>er>d>fault

What is it that I am doing wrong in this case?

1 answer

  • answered 2018-01-14 11:58 sjsam

    Use an array

    var=( "$(curl -ksi http://localhost/)" )
    echo "${var[@]}"
    

    Side-Note: Don't use all uppercase variables like VAR as user variables as they are reserved for the shell environment