How to match a percentage regex in bash?

Member

by ryleigh , in category: Third Party Scripts , 3 days ago

How to match a percentage regex in bash?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 3 days ago

@ryleigh 

To match a percentage using a regex in bash, you can use the following pattern:

1
2
3
4
5
if [[ $input =~ ^[0-9]+%$ ]]; then
    echo "Percentage match found: $input"
else
    echo "No percentage match found"
fi


In this regex pattern:

  • ^ indicates the start of the string
  • [0-9]+ matches one or more digits
  • % matches the percentage symbol
  • $ indicates the end of the string


You can use this pattern in a bash script to check if a string contains a percentage at the end. Replace $input with the variable containing the string you want to check. If a percentage match is found, it will print the matched percentage value.