Skip to content

Instantly share code, notes, and snippets.

@sandrock
Last active March 28, 2024 08:12
Show Gist options
  • Save sandrock/5245d16a7d880e4662d2e1b3964850a2 to your computer and use it in GitHub Desktop.
Save sandrock/5245d16a7d880e4662d2e1b3964850a2 to your computer and use it in GitHub Desktop.
bash parse semantic version
#!/bin/bash
value=v1.20.03-xx7
semver=$(echo "$value" | sed -r "s/^v?([0-9]+)\.([0-9]+)\.([0-9]+)(.*)$/\1 \2 \3 \4/") ; semver=( $semver )
echo "result: ${semver[0]} ${semver[1]} ${semver[2]} ${semver[3]}"
@sandrock
Copy link
Author

The sed command is used to set a space separator between each part. The ( ) generates an array composed of each part.

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