0

Here is a shell puzzle I can't answer (bash).

This returns exit code 1:

ping $HOSTNAME -c2 -W2 2&>1 > /dev/null

This returns the correct exit code every time (2, 1 or 0).

ping $HOSTNAME -c2 -W2

Why is the first one always returning 1? I'm just trying to hide/throwaway all the output of the ping command.

1

Looks like the order should be adjusted to:

ping "$myHost" -c2 -W2 > /dev/null 2>&1; echo $?
0

Answered here. Essentially, the first part > /dev/null sends stdOut to /dev/null, then you re-route stdErr to stdOut.

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.