Nilsen Chris Nilsen Chris - 1 year ago 101
Bash Question

How do i check if the $# parameter with URL starts with http/https and ends with jpg/jpeg?

i found this earlier but how do i check if my url starts with http/https and ends with jpg/jpeg

i need too check:

starts with: http https

ends with: jpg jpeg JPG JPEG


`if [[ $1 = http?(s)://*.jpg ]];
echo "invalid URL"

Answer Source

You can use this BASH regex:

[[ ${1,,} =~ ^https?://.+\.jpe?g$ ]]
  • ${1,,} is to convert $1 to all lowercase
  • regex checks if $1 has http:// or https:// at start and it ends with jpg or jpeg
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download