Parsable Swift version from command line?

swift -version yields a very human readable version string:

Apple Swift version 5.1.2 (swiftlang-1100.0.278 clang-1100.0.33.9)
Target: x86_64-apple-darwin19.0.0

It's a little hard to make sense of in a CI context, though, where shell scripts, rather than Swift code, need to understand the build environment.

Right now I'm parsing it with bash regex like so:

[[ `swift -version` =~ ([0-9]+\.[0-9]+(\.[0-9]+)?) ]]; echo $BASH_REMATCH

But how consistent is the format of this version string? Is there a better way?

1 Like

I don't think there is, but there is a flag -parseable-output for this purpose. Perhaps the combination swiftc --version -parseable-output should return the version as a JSON object; right now it seems to ignore the -parseable-output flag.

Also, watch out for this: