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

<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>

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

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