Semantic Versioning (SemVer) Parser and Comparator
This module provides semantic versioning (SemVer 2.0.0) support including parsing, comparison, constraint checking, and version sorting.
- demo: demo.semver.sh, demo.sorting.sh, demo.sorting.v2.sh
- bin: git.semantic-version.sh, version-up.v2.sh, install.e-bash.sh
- documentation: Referenced in docs/public/version-up.md
- tests: spec/git_semantic_version_spec.sh, spec/self_update_version_spec.sh
Compare two semantic versions
| Name | Type | Default | Description |
|---|---|---|---|
version1 |
string | required | First version string |
version2 |
string | required | Second version string |
- reads/listen: __semver_compare_v1, __semver_compare_v2
- mutate/publish: creates temporary associative arrays for comparison
- 0 if versions are equal
- 1 if version1 > version2
- 2 if version1 < version2
- 3 if error (invalid version)
semver:compare "1.2.3" "1.2.4" # Returns 2
semver:compare "2.0.0" "1.9.9" # Returns 1
Implementation of https://semver.org/#spec-item-11 specsGenerate human-readable version comparison output
| Name | Type | Default | Description |
|---|---|---|---|
version1 |
string | required | First version string |
version2 |
string | required | Second version string |
- reads/listen: none
- mutate/publish: none
- Echoes formatted string: "version1 operators version2"
semver:compare:readable "1.2.3" "1.2.4" # Returns "1.2.3 < <= != 1.2.4"Convert semver:compare result code to operator string
| Name | Type | Default | Description |
|---|---|---|---|
result |
number | required | Result code from semver:compare (0, 1, 2, 3) |
- reads/listen: none
- mutate/publish: none
- Echoes operator string: "==", ">", "<", or "error"
semver:compare "1.2.3" "1.2.3"
operator=$(semver:compare:to:operator "$?") # Returns "=="Main entry point for semantic version constraint checking
| Name | Type | Default | Description |
|---|---|---|---|
version |
string | required | Version string to check |
expression |
string | required | Constraint expression |
- reads/listen: SEMVER_CONSTRAINTS_IMPL (v1 or v2, default: v2)
- mutate/publish: none
- 0 if version satisfies constraints
- 1 if version does not satisfy constraints
semver:constraints "1.2.3" ">=1.0.0 <2.0.0"
SEMVER_CONSTRAINTS_IMPL=v1 semver:constraints "1.2.3" "^1.0.0"Expand tilde (~) and caret (^) constraint operators to simple expressions
| Name | Type | Default | Description |
|---|---|---|---|
molecule |
string | required | Constraint expression with ~ or ^ operators |
- reads/listen: none
- mutate/publish: creates temporary __semver_constraints_complex_atom array
- 0 on success, 1 on parse error
- Echoes one or two constraint expressions
semver:constraints:complex "~1.2.3" # Returns ">=1.2.3" and "<1.3.0"
semver:constraints:complex "^1.0.0" # Returns ">=1.0.0" and "<2.0.0"Simple semantic version constraint check
| Name | Type | Default | Description |
|---|---|---|---|
expression |
string | required | Constraint expression (e.g., "1.2.3>=1.0.0") |
- reads/listen: none
- mutate/publish: none
- 0 if constraint satisfied
- 1 if constraint not satisfied
- 3 if expression invalid
semver:constraints:simple "1.2.3>=1.0.0" # Returns 0
The basic comparisons are:
=: equal (aliased to no operator)
!=: not equal
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to
ref: https://github.com/Masterminds/semver?tab=readme-ov-file#basic-comparisonsVerify version matches constraints (v1 - legacy behavior)
| Name | Type | Default | Description |
|---|---|---|---|
version |
string | required | Version string to check |
expression |
string | required | Constraint expression |
- reads/listen: none
- mutate/publish: none
- 0 if version satisfies constraints
- 1 if version does not satisfy constraints
semver:constraints:v1 "1.2.3" ">=1.0.0"Verify version matches constraints (v2 - npm-like with prerelease handling)
| Name | Type | Default | Description |
|---|---|---|---|
version |
string | required | Version string to check |
expression |
string | required | Constraint expression |
- reads/listen: none
- mutate/publish: creates temporary _semver_constraints_v2* arrays
- 0 if version satisfies constraints
- 1 if version does not satisfy constraints
semver:constraints:v2 "1.2.3-alpha" ">=1.0.0"Generate semantic versioning regex pattern
| Name | Type | Default | Description |
|---|
- reads/listen: none
- mutate/publish: none
- Echoes extended regex pattern for semver matching
pattern=$(semver:grep)
ref: https://regex101.com/r/vkijKf/1/,
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$
\d - any digit
ref: https://semver.org/#backusnaur-form-grammar-for-valid-semver-versionsIncrement major version number (resets minor and patch to 0)
| Name | Type | Default | Description |
|---|---|---|---|
version |
string | required | Version string to increment |
- reads/listen: none
- mutate/publish: creates temporary __major associative array
- Echoes incremented version (major+1.0.0)
semver:increase:major "1.2.3" # Returns "2.0.0"Increment minor version number (resets patch to 0)
| Name | Type | Default | Description |
|---|---|---|---|
version |
string | required | Version string to increment |
- reads/listen: none
- mutate/publish: creates temporary __minor associative array
- Echoes incremented version (major.minor+1.0)
semver:increase:minor "1.2.3" # Returns "1.3.0"Increment patch version number
| Name | Type | Default | Description |
|---|---|---|---|
version |
string | required | Version string to increment |
- reads/listen: none
- mutate/publish: creates temporary __patch associative array
- Echoes incremented version (major.minor.patch+1)
semver:increase:patch "1.2.3" # Returns "1.2.4"Parse semantic version string into components
| Name | Type | Default | Description |
|---|---|---|---|
version |
string | required | Version string to parse |
output_variable |
string | default: "__semver_parse_result" | Name of associative array to store results |
- reads/listen: SEMVER (regex pattern)
- mutate/publish: creates global associative array with components
- 0 if version matches semver pattern, 1 otherwise
- Populates output array with: version, version-core, major, minor, patch, pre-release, build
semver:parse "1.2.3-alpha+build" "result"Create version string from parsed semver components
| Name | Type | Default | Description |
|---|---|---|---|
sourceVariableName |
string | default: "__semver_parse_result" | Name of associative array with parsed parts |
- reads/listen: associative array with semver components
- mutate/publish: none
- Echoes formatted version string (major.minor.patch-pre-release+build)
semver:parse "1.2.3-alpha" "parsed"
version=$(semver:recompose "parsed")