How to print an sbt project’s version from the command line

When you want to print an sbt project’s version number from the command line — and only the version number — Seth Tisue came up with this answer here on the sbt gitter channel:

sbt -Dsbt.supershell=false -error "print version"

If your sbt project is at version 1.0 — i.e., the version variable is set to 1.0 — that prints this output:

1.0

Background

Just before that point on the gitter channel, Guillaume Martres asked if there was a way to just print an sbt project’s version. As he mentions, when you run sbt "show version", it prints a bunch of information before showing the version:

> sbt "show version"
[info] welcome to sbt 1.4.4 (AdoptOpenJDK Java 11.0.9)
[info] loading settings for project publishing-scala3-build from plugins.sbt ...
[info] loading project definition from ...
[info] loading settings for project publishing-scala3 from build.sbt ...
[info] set current project to StringUtils ...
[info] 1.0

When all you want is the 1.0 part of the output, use the first sbt command shown.

If you want to learn things about sbt on almost a daily basis, I recommend joining the sbt gitter channel.