Skip to content

Instantly share code, notes, and snippets.

@mtracz
Created July 22, 2021 11:20
Show Gist options
  • Select an option

  • Save mtracz/f4eda4131ca38bb88aed400e35ed2d47 to your computer and use it in GitHub Desktop.

Select an option

Save mtracz/f4eda4131ca38bb88aed400e35ed2d47 to your computer and use it in GitHub Desktop.
bash: explode string into array by delimiter
#!/bin/bash
VERSION=1.2.3.4
oldIFS=$IFS
# delimiter
IFS="."
#Read the split words into an array based on delimiter
read -a VERSION_ARRAY <<< $VERSION
IFS=$oldIFS
VERSION_PATH="${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}.${VERSION_ARRAY[2]}"
VERSION_MINOR="${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}"
VERSION_MAJOR="${VERSION_ARRAY[0]}"
echo VERSION_PATH=$VERSION_PATH
echo VERSION_MINOR=$VERSION_MINOR
echo VERSION_MAJOR=$VERSION_MAJOR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment