-1

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?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
vovacodes
  • 525
  • 4
  • 14

1 Answers1

0

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

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • 1
    The array will only contain a single element. It brings no value over a simple string variable. – tripleee Jan 14 '18 at 12:24
  • I agree.. I was thinking about a specific use case, which in fact I saw in some answer in SO.. But I couldn't link it though. – sjsam Jan 14 '18 at 12:28
  • thanks for the side note! The answer is not entirely right though, not quoting the variable was the issue – vovacodes Jan 14 '18 at 16:47
  • Indeed, but you got the point right? :-) So all, good – sjsam Jan 14 '18 at 16:48