echo stderr and stdout to the file from the script variable bash

advertisements

I have a bash script and inside this bash script I have a JAVARESULT variable like this :

JAVARESULT=`java -cp ... parser_file $file $someextravar`

and what I want is to catch in a log file the stderr and stdout of this result variable.

echo "$JAVARESULT" > $LOG_FILE

but i get only the stdout not the stderr. I tried with :

echo "$JAVARESULT" &> $LOG_FILE

but I don't get the java errors in the log file .


In every Unix based system every process have at least three file descriptors open. As you know, file descriptors are identified by numbers. This three standard file descriptors are:

  • 0 for stdin
  • 1 for stdout
  • 2 for stderr

What you want to do is redirect stderr to stdout, and then redirect stdout to a file. So, in your JAVARESULT variable you'll just have to append:

2>&1

What you're saying here is: redirect stderr (file descriptor 2) to stdout (file descriptor 1).