Arie Shterengartz Arie Shterengartz - 1 month ago 8
Bash Question

Get java version in csh (c shell)

I need to get my java version using a c-shell script.
I'll need to put it into a variable and use it afterwards for some manipulations and tests.
in bash this command works:

local javaVersion=$(java -version 2>&1 | sed 's/java version "\(.*\)\.\(.*\)\..*"/\1\2/; 1q')

but in c-shell, when I try:

set javaVersion=$(java -version 2>&1 | sed 's/java version "\(.*\)\.\(.*\)\..*"/\1\2/; 1q')

I get

"Ambiguous output redirect."


Yes, I have to do it in c-shell, not Bash or any other language.

I searched this and other forums in the internet but didn't find anything helpful.



Here is a way that should works for you as it does for me:

> set javaVersion=`java -version |& sed 's/.* version "\(.*\)\.\(.*\)\..*"/\1\2/; 1q'`
> echo $javaVersion

Changes are:

  • Replace $( command ) by `command`; the former is the recommended current POSIX shell syntax but has never been implemented by csh.

  • Replace 2>&1 | by |&; the former is Bourne shell specific, the latter is csh specific.

  • Replace java version by .* version; this is not strictly necessary but eased my tests as java -version returns openjdk version... on my machine, not java version...