1

With the release of macOS 10.15 Catalina, the default shell will be switched from bash to zsh. While reviewing some scripts and documentation, I realized that parameter expansion changes would need to be made.

Following the question bash regex to match semantic version number, I needed to update my method of collecting semantic version numbers.

In zsh, how can I split semantic version numbers using parameter expansion?

1 Answer 1

1

While using my previous answer as a template, I was able to convert my code to use zsh parameter expansion.

product_version=$(sw_vers -productVersion) # 10.14.6
os_vers=( ${(@s:.:)product_version} ) # ( 10 14 6 )
os_vers_major="${os_vers[1]}" # 10
os_vers_minor="${os_vers[2]}" # 14
os_vers_patch="${os_vers[3]}" # 6
os_vers_build=$(sw_vers -buildVersion) #18G87
echo "${os_vers_major}.${os_vers_minor}.${os_vers_patch}+${os_vers_build}" # 10.14.6+18G87

Note that (by default) zsh arrays start at index 1 not 0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.