I'm doing some audit automation, this example describes checking the version of Java, even though other programs do the same thing.

The output of "java -version" goes to STDERR, which is easily redirected, but I want to send shell errors (for example, when the java binary is missing) to /dev/null. It seems that shell redirection is an all-or-nothing proposition.

My most promising attempt so far as been:

{ /bin/ksh "{java -version 2>&1;}"; } 2>/dev/null

...which properly sends the output of the -version command to STDOUT, but if java isn't there, it sends the shell "not found" error to STDOUT as well. I don't want to see that message.

Same behavior with:

{ /bin/ksh "{java -version 2>&1;}" 2>/dev/null; }

Does anyone know a way to limit the scope of redirection so each process gets its own? I'm not limited to ksh, but for environment reasons it's got to be a shell based one-liner.

2 Answers 2


Is it not possible to check if java is found and executable?

test -x "$(which java)" && java -version 2>&1

or if you want to get around test and check for java-errors, too

java -version 2>/dev/null 1>/dev/null && java -version 2>&1
  • Using "test" is a different, promising approach. That could do the trick. Jun 13, 2012 at 19:12

You can redirect to additional descriptors. This is for bash, I think it works for ksh too:

exec 3>javalog
java ... 2>&1 >&3
exec 3>&-

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.