How to suppress cUrl's progress meter when redirecting the output?
Solution 1
Try this:
curl -vs -o /dev/null http://somehost/somepage 2>&1
That will suppress the progress meter, send stdout
to /dev/null
and redirect stderr
(the -v
output) to stdout
.
Solution 2
curl --fail --silent --show-error http://www.example.com/ > /dev/null
This will suppress the status dialog, but will otherwise output errors to STDERR.
user@host:~# curl http://www.yahoo.com > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 254k 0 254k 0 0 403k 0 --:--:-- --:--:-- --:--:-- 424k
The above outputs the status table when redirecting.
user@host:~# curl --fail --silent --show-error http://www.yahoo.com > /dev/null
The above suppresses the status table when redirecting, but errors will still go to STDERR.
user@host:~# curl --fail --silent --show-error http://www.errorexample.com > /dev/null
curl: (6) Couldn't resolve host 'www.errorexample.com'
The above is an example of an error to STDERR.
user@host:~# curl -v --fail --silent --show-error http://www.errorexample.com > ~/output.txt 2>&1
user@host:~# cat ~/output.txt
* getaddrinfo(3) failed for www.errorexample.com:80
* Couldn't resolve host 'www.errorexample.com'
* Closing connection #0
curl: (6) Couldn't resolve host 'www.errorexample.com'
Just add 2>&1 to the end to redirect STDERR to STDOUT (in this case, to a file).
Solution 3
According to man curl
:
-s, --silent : Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute.
Example usage:
curl -s 'http://www.google.com'
or if you want to capture the HTTP-BODY into a variable in bash
BODY=$( curl -s 'http://www.google.com' )
echo $BODY
You can use -s
or --silent
interchangeably.
Solution 4
With reference to question 1 (how cURL knows to only display the table when output is redirected), I didn't realise a program could tell its outputs were being directed, but it seems on POSIX systems there is a function isatty
which reports whether or not a file descriptor refers to a terminal.
Solution 5
Modern versions of curl
now have the option --no-progress-meter
which disables only the progress meter you are referencing.
Related videos on Youtube
Aishwarya R
Updated on September 17, 2022Comments
-
Aishwarya R over 1 year
I'm trying to print just the verbose sections of a cURL request (which are sent to
stderr
) from the bash shell.But when I redirect
stdout
like this:curl -v http://somehost/somepage > /dev/null
Some sort of results table appears in the middle of the output to
stderr
:% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
Followed by this near the end:
{ [data not shown] 118 592 0 592 0 0 15714 0 --:--:-- --:--:-- --:--:-- 25739
Which makes the response headers less readable.
I don't see this text when not redirecting.
Another way to see the effects:
Table doesn't appear:
curl -v http://somehost/somepage 2>&1
Table appears:
curl -v http://somehost/somepage 2>&1 | cat
How come this shows up only with certain types of redirects?
What's the neatest way to suppress it?
-
Aishwarya R almost 14 yearsThanks,
-s
was the key! -
Dennis Williamson almost 14 yearsHere's a Bash snippet:
[[ -p /dev/stdout ]] && echo "stdout is to a pipe"; [[ -t 1 ]] && echo "output to terminal"; [[ ! -t 1 && ! -p /dev/stdout ]] && echo "output redirected"
-
Artyom about 10 years@IanMackinnon Note that with
-s
but without-v
you will not see errors such as failure to connect. For that you should also add-S
(or--show-error
) as in mhoydis's answer. -
sixty4bit almost 5 yearsbut why does the progress bar only appear in the first place when redirecting? I ran into this same issue when piping the output of
curl
tojq
. No progress bar without piping tojq
, then when piping tojq
I have to go back and add-s
. -
Dennis Williamson almost 5 years@sixty4bit: That's a design choice the developers made. The program can detect when its
STDOUT
isn't a tty. When output is not being piped, you don't want progress information to be interspersed with normal output, which you can see and have some idea of progress. When output is redirected or piped, you can't see it so you have no gauge for progress - unless the progress bar is turned on. -
siikamiika almost 4 yearsAccording to the cURL manpage, this feature was added in 7.67.0 which was released on November 6 2019.