Skip to content

Instantly share code, notes, and snippets.

@bitmvr
Last active May 24, 2024 08:29
Show Gist options
  • Save bitmvr/9ed42e1cc2aac799b123de9fdc59b016 to your computer and use it in GitHub Desktop.
Save bitmvr/9ed42e1cc2aac799b123de9fdc59b016 to your computer and use it in GitHub Desktop.
A POSIX Compliant SemVer Parser in Pure Bash
#!/usr/bin/env bash
VERSION="${1#[vV]}"
VERSION_MAJOR="${VERSION%%.*}"
VERSION_MINOR_PATCH="${VERSION#*.}"
VERSION_MINOR="${VERSION_MINOR_PATCH%%.*}"
VERSION_PATCH_PRE_RELEASE="${VERSION_MINOR_PATCH#*.}"
VERSION_PATCH="${VERSION_PATCH_PRE_RELEASE%%[-+]*}"
VERSION_PRE_RELEASE=""
case "$VERSION" in
*-*)
VERSION_PRE_RELEASE="${VERSION#*-}"
VERSION_PRE_RELEASE="${VERSION_PRE_RELEASE%%+*}"
;;
esac
echo "Version: ${VERSION}"
echo "Version [major]: ${VERSION_MAJOR}"
echo "Version [minor]: ${VERSION_MINOR}"
echo "Version [patch]: ${VERSION_PATCH}"
echo "Version [pre-release]: ${VERSION_PRE_RELEASE}"
@bitmvr
Copy link
Author

bitmvr commented May 23, 2024

Okay, I've patched it. I will add comments later around what's happening where as parameter expansion in Bash can be confusing.

@mman
Copy link

mman commented May 24, 2024

@bitmvr Cool thanks a lot, I think that as much as SemVer is popular, we actually (if I am not mistaken) miss generally well available installed everywhere typ of tool to actually parse semver, to use in GitHub actions, and build scripts, etc. so your script is actually very handy :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment