Okay, here's a common and effective shell command using curl and jq to get the latest release tag name from a GitHub repository:
# --- Configuration ---
# Replace OWNER and REPO with the target repository's owner and name
OWNER="docker"
REPO="cli"
# -------------------
# --- Command to get the latest release tag ---
LATEST_TAG=$(curl -s "https://api.github.com/repos/${OWNER}/${REPO}/releases/latest" | jq -r '.tag_name')
# --- Output the result ---
echo "Latest release tag for ${OWNER}/${REPO}: ${LATEST_TAG}"
Explanation:
OWNER="..." and REPO="...": Set variables for the repository owner and name. You must change these to the specific repository you're interested in.
curl -s "URL":
curl: A command-line tool to transfer data using various protocols, including HTTPS.
-s or --silent: Makes curl silent. It won't show progress meters or error messages (like 404 Not Found if the repo/release doesn't exist), just the data fetched.
"https://api.github.com/repos/${OWNER}/${REPO}/releases/latest": This is the GitHub API endpoint to get metadata about the latest non-prerelease, non-draft release for the specified repository.
|: The pipe symbol sends the output (the JSON data from curl) as input to the next command.
jq -r '.tag_name':
jq: A lightweight and flexible command-line JSON processor. It's essential for parsing the JSON response from the GitHub API.
-r or --raw-output: Outputs the string value directly, without JSON quotes around it.
.tag_name: This is the jq filter to extract the value associated with the tag_name key from the JSON object. This key typically holds the Git tag associated with the release (e.g., v20.10.7).
LATEST_TAG=$(...): This uses command substitution to capture the output of the curl | jq pipeline into the LATEST_TAG shell variable.
echo "...": Prints the result to the console.
Prerequisites:
curl: Usually pre-installed on most Linux distributions and macOS.
jq: You might need to install this.
- Debian/Ubuntu:
sudo apt update && sudo apt install jq
- Fedora/CentOS/RHEL/Amazon Linux:
sudo yum install jq or sudo dnf install jq
- macOS (using Homebrew):
brew install jq
Alternative: Get Release Name
If you want the human-readable name of the release instead of the tag, change the jq filter:
LATEST_NAME=$(curl -s "https://api.github.com/repos/${OWNER}/${REPO}/releases/latest" | jq -r '.name')
echo "Latest release name for ${OWNER}/${REPO}: ${LATEST_NAME}"
Choose the command based on whether you need the tag (like v1.2.3) or the release title (like "Version 1.2.3 Release").