Question

I already searched several answers, but those cannot be applied to my problem.

The problem code is below.

I think the result of dart -version is not a simple string.

DART_VER=$(dart --version)
#expect output as ==>> Dart VM version: ...
echo "${DART_VER}"

if [[ "$DART_VER" == Dart* ]]
then
 echo Dart!   # doesn't enter here
fi
Was it helpful?

Solution

I believe that dart --version is writing out to standard error rather than standard output. (See https://code.google.com/p/dart/codesearch#dart/trunk/dart/runtime/bin/main.cc&sq=package:dart&q=ProcessVersionOption&l=588.) So when you see your script print Dart VM version: ..., that's actually coming from the DART_VER=$(dart --version) (which prints Dart VM version: ... to standard error, and sets DART_VER to the empty string) rather than the echo "${DART_VER}" (which just prints the empty string, plus a newline).

You can fix that by changing this:

DART_VER=$(dart --version)

to this:

DART_VER=$(dart --version 2>&1)

to merge standard error into standard output for capture.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top