I am executing a shell script in which one curl command is present. I have to store the output of curl command(xml format) in a file and display the result on screen. For this to happen "tee command" is used after curl using pipeline. I need to check if the curl command is executing successfully or not. I used $? but it is taking the exit status of tee command. I used PIPESTATUS[0] and set -o pipefail but did not work for curl. How do i check if the curl command is executing successfully or not? How do I store the exit status of curl command in a variable? This is just a demo code.

curl output | tee -a filename

I want to know the exit status of curl


Use the $PIPESTATUS var which is an array. Following will give you the return code of the piped command.

curl output | tee -a filename
echo ${PIPESTATUS[0]}
  • +1, but requires a bash warning. – Jan Hudec May 30 '14 at 20:03

You could use the following construction:

content=`curl http://www.google.com`
if [[ $? -ne 0 ]]; then 
  # do some fail stuff
  echo '$content' | tee /tmp/output.file

It stores the output of curl in a variable, then checks the exit status, and if it's successful, tee the output to a file (and stdout).

  • echo? no quotes? on something received from network? seriously? -1. – Jan Hudec May 30 '14 at 19:58
  • @JanHudec You're right about the quotes, I assumed he is going to pull something from a page he trusts (I edited the post). You're more than welcome to edit my answer so we can all learn from your wisdom – mtak May 31 '14 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.