Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash Regex if else

In Bash I’m trying to check if a string is in the appropriate format.

#!/bin/bash

COMMIT_MSG="release/patch/JIRA-123"

[[ $COMMIT_MSG =~ 'release\/(major|minor|patch)\/[A-Z\d]+-\d+' ]] && echo "yes" || echo "no"

This is the regex I’ve used to match the string as patch could be either major or minor and JIRA-123 is Jira Ticket ID but when trying it in the Bash regex it always returns no.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Bash is a simplified version of regex called "Extended Regular Expression". \d doesn’t exist in it, so use [0-9] instead.

Additionally, you shouldn’t quote the regex in the condition.

[[ $COMMIT_MSG =~ release/(major|minor|patch)/[A-Z0-9+]+-[0-9]+ ]] && echo "yes" || echo "no"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading