Home > Blockchain >  Bash Regex if else
Bash Regex if else

Time:08-26

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.

CodePudding user response:

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"
  •  Tags:  
  • bash
  • Related