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?

PinkFloyd
  • 419
  • 7
  • 18
  • 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 07 '15 at 01:57

1 Answers1

4

> 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 its 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 
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
jordanm
  • 41,988
  • 9
  • 116
  • 113
  • "> 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 07 '15 at 02:05