Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question
    
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 at 1:57

1 Answer 1

up vote 2 down vote accepted

> 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 
share|improve this answer
    
"> 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 at 2:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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