0

I have a command such that

bar > /dev/null

and I want to know the exit status of bar. I read some posts su about ${PIPESTATUS[0]} but this works when one pipes the output via | and I can't make it work with > instead.

What am I missing?

  • From the same question you have referred, you could try false > /dev/null and see that only ${PIPESTATUS[0]} has a value and ${PIPESTATUS[1]} is null which says the entire bar > /dev/null is a single command with no pipe involved. – Ramesh Jan 7 '15 at 1:57
2

> isn't a command. This means that bar will be the last command executed. You can check for failure with a standard if statement:

if ! bar > /dev/null; then
    echo "bar command failed"
fi

You can also access it's return code with $? if you are interested in something more than zero or non-zero:

bar > /dev/null
if [ $? -eq 45 ]; then
  echo "bar returned exit code 45"
fi 
  • "> isn't a command. This means that bar will be the last command executed." That is a strange way to describe it. You can have >/dev/null before the last command in a pipeline. That can even make sense. The point is that > doesn't create a pipeline. – Hauke Laging Jan 7 '15 at 2:05

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.