I am new to scripting.... i want to write a simple script file that executes the following command `java -version`.... i have to read the output given by this command inside my script....

var_name=`java -version`
echo $var_name

just displays java -version
i want the output of java -version command
what should i do


That should actually work. The backticks should return the output of the command rather than just repeat what's between them. It seems as if the backticks are forward ticks.

Try copying and pasting this, just in case it's a small mis-type like that. Otherwise, you're not doing anything wrong that I can see. You're right on target :)

var_name=`java -version`
echo $var_name

Also, what shell are you using, in case that isn't the issue?

Best wishes,


It seems that java -version sends its output to stderr, at least in my case.
Just try this:

version=`java -version 2>&1`

and see if it works better for you.

Ya its working now... whats that 2>&1.. whats that indicate... can u tell me

1 stands for the standard output, it is the default output that you catch with ">".
2 stands for the standard error, specifying "2>&1" means that you redirect the standard error to the same as the standard output.

Ha ha :)

Good catch. That little script I ran spat out the output I expected and I didn't even think of that :) Sure enough

var_name=`java -version 2>/dev/null`
echo $var_name

produced no results.

Nice catch, again :)

, Mike