Reputation: 197
Why am I not getting the same result when I do :
1. Go to my terminal and enter
curl -I www.httpbin.org
Result :
HTTP/1.1 200 OK
Connection: keep-alive
Server: meinheld/0.6.1
Date: Wed, 20 Dec 2017 19:20:56 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 13011
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.00580310821533
Via: 1.1 vegur
2. Create a file APIConnection.sh
Containing :
#! /bin/bash
api_url_1="www.httpbin.org"
echo `curl -I $api_url_1`
Then go to my terminal and execute the file : ./APIConnection.sh
Result :
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 13011 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
Via: 1.1 vegurme: 0.0147929191589 true
Upvotes: 1
Views: 14219
Reputation: 12393
The reason for which a progress bar is only displayed when running curl
between backticks is explained in man curl
:
PROGRESS METER
curl normally displays a progress meter during operations, indicating the amount of transferred data, transfer speeds and estimated time left, etc. The progress meter displays number of bytes and the speeds are in bytes per second. The suffixes (k, M, G, T, P) are 1024 based. For example 1k is 1024 bytes. 1M is 1048576 bytes. curl displays this data to the terminal by default, so if you invoke curl to do an operation and it is about to write data to the terminal, it disables the progress meter as otherwise it would mess up the output mixing progress meter and response data.
If you want to turn off the progress meter explicitly, this can be done with --silent
.
``
is legacy syntax for command substitution in bash and other POSIX-compliant shells. This means that output is captured by the shell, not written directly to the terminal. Because curl
cannot detect that it would later be written to the terminal by echo
, it does not disable the progress meter as described above.
If you really do want to use a command substitution here (instead of just having your script run curl -I "$api_url_1"
without any command substitution or echo), put quotes around your command substitution to avoid string-splitting and glob expansion.
It's also advisable to use the modern command substitution syntax, $()
, so each substitution has its own quoting context:
echo "$(curl --silent -I "$api_url_1")"
Upvotes: 3
Reputation: 639
executing curl -I www.httpbin.org
gives you the result of the standard output while executing echo `curl -I $api_url_1`
gives you the result of the standard error.
To see the differences execute the following command and then look at the content of created se.txt
and so.txt
files:
curl -I www.httpbin.org 1>so.txt 2>se.txt
look at about stdin, stdout and stderr for more information.
Upvotes: 0