How to programmatically find the latest version number of Homebrew?
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($&lt;.read)["name"]')"

    # Test that both variables hold the expected pattern.

if [[ $localHomebrewVersion =~ [0-9]\.[0-9]{1,2}\.[0-9]{1,2} ]] &amp;&amp; [[ $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.