2

How to programmatically find the latest version number of Homebrew?

I can find this on my local computer with brew --version and string process the result to get a version number. What's a good way to get it for the released Homebrew? I'd like to programmatically verify the homebrew is up-to-date for many computers.

3
  • This looks to be three questions? a) How to get homebrew version from the web site where it's released. b) how to compare version on one computer with the release c) how to manage many computers (whether via MDM, Apple Remote Desktop, scripting and ssh, etc...) Would you please edit this to make clear which one thing you need to learn in this thread? Asking all three separately would be ideal so we could link to existing questions or provide one good answer to one part without needing to contemplate all three hurdles you might face.
    – bmike
    Mar 27 '17 at 18:39
  • 2
    I don't think I really asked about your items b and c. That's just to provide some background to the question I'm asking so some potential readers/answerers wouldn't wonder why I'm trying to do this.
    – qazwsx
    Mar 27 '17 at 20:49
  • 1
    @bmike I feel it is sufficiently clear what is being asked here. The verbosity simply serves to provide a little more context. This is how I interpret the question, construction-wise: “How do I do X? I already know how to Y. But what’s a good way to do X? (I need X and Y to accomplish goal A.)” – The only thing that is actually being asked is X: how to get the version number of the latest release of Homebrew.
    – Synoli
    Mar 27 '17 at 22:06
2

I personally do not use Homebrew, so if there is a way using brew itself to get the latest version number available online, I don't know it.

Note that since Jun 27, 2012 and version 0.9.1 of Homebrew, the version numbering has maintained a set typical pattern of major.minor.maintenance, using only numbers separated by a period. So this is relatively easy to test that values returned from the command substitutions used to assign them to both the installed version and the latest release version variables follow this pattern.

Once tested, the version strings are converted to numbers so that a binary comparison can determine if the installed version is less than the latest release version, at which point one can take appropriate action.

The following bash script is an example of how one might code it:

#!/bin/bash

    # Get installed version number and latest release version number.

localHomebrewVersion="$(brew --version | awk '/Homebrew [0-9]/{print $2}')"
latestHomebrewRelease="$(curl -sL https://api.github.com/repos/Homebrew/brew/releases/latest | ruby -rjson -e 'puts JSON.parse($<.read)["name"]')"

    # Test that both variables hold the expected pattern.

if [[ $localHomebrewVersion =~ [0-9]\.[0-9]{1,2}\.[0-9]{1,2} ]] && [[ $latestHomebrewRelease =~ [0-9]\.[0-9]{1,2}\.[0-9]{1,2} ]]; then

        # Function used to convert string version numbers to numeric values for testing.

    function version { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d", $1,$2,$3,$4); }'; }

        # Test to see if the installed version number is less than the latest release version.

    if [[ $(version $localHomebrewVersion) -lt $(version $latestHomebrewRelease) ]]; then
        brew update
    else
        echo "Homebrew is already up-to-date."
    fi

else
        # One or both of the command substitutions '$(...)' returned unexpected output for the version variables.

    echo "At least one of the version variables contains an unexpected value."
fi

Note: I've incorporated the command suggested in Synoli's comment to assign to the latestHomebrewRelease variable, as it certainly appears to be a more stable method to get the latest release version information.

Update: Added a test to see the version variables contain the expected pattern before testing if the installed version is less than the latest release version.

2
  • 1
    GitHub has an API for this, so a more stable method would be: curl -sL https://api.github.com/repos/Homebrew/brew/releases/latest | ruby -rjson -e 'puts JSON.parse($<.read)["name"]'
    – Synoli
    Mar 28 '17 at 7:47
  • 1
    @Synoli, Thanks for the command suggestion in your comment. I've incorporated it into my answer. Mar 28 '17 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .