Coding with Titans

so breaking things happens constantly, but never on purpose

HowTo: Get currently active version of Xcode

Sometimes knowing the current version of Xcode used to build the stuff from command line becomes a useful intel. Here is the recipe on how to obtain this information in a clean manner. Easiest command would be to call:

$ xcodebuild -version

That produces output like:

Xcode 12.5
Build version 12E262

As an improvement I could use sed to simply extract the number from a line with the Xcode prefix:

$ xcodebuild -version | sed -n -E 's/Xcode ([0-9.]*).*/\1/p;'

That in current case outputs:

12.5

So far so good. I can even get rid of the dot, if it’s not need in certain cases by processing the extracted number further:

$ xcodebuild -version | sed -n -E 's/Xcode ([0-9\.]+).*/\1/p;' | sed 's/\.//'

To receive result as:

125

Finally I could use this value for some other extra processing, if it’s first set as a variable inside a shell script like following:

#!/bin/sh

xcode_version=$(xcodebuild -version | sed -n -E 's/Xcode ([0-9\.]+).*/\1/p;')
echo "Current Xcode: $xcode_version"

Awesome! Thanks.