How do I pipe or redirect the output of curl -v?

后端 未结 8 1786
挽巷
挽巷 2021-01-29 21:17

For some reason the output always gets printed to the terminal, regardless of whether I redirect it via 2> or > or |. Is there a way to get around this? Why is this happening?

8条回答
  •  被撕碎了的回忆
    2021-01-29 21:49

    This simple example shows how to capture curl output, and use it in a bash script

    test.sh

    function main
    {
      \curl -vs 'http://google.com'  2>&1
      # note: add -o /tmp/ignore.png if you want to ignore binary output, by saving it to a file. 
    }
    
    # capture output of curl to a variable
    OUT=$(main)
    
    # search output for something using grep.
    echo
    echo "$OUT" | grep 302 
    echo
    echo "$OUT" | grep title 
    

提交回复
热议问题