I have url like example.com/app1/ID_123
or example.com/app2/ID_123
stored in a shell variable named my_url
or array. How do I check if a string contains a substring such as app1
or app2
in Bash? Can I do grep app1 $my_url
? The grep one is not working out for me. Hence, I wanna know How to find substring inside a string and how to grep a variable?
Try the case…esac statment
case "$my_url" in
*app1* ) echo "app1 found";;
*app2* ) echo "app2 found";;
* ) echo "Error, app1 or app2 not found in $my_url";;
esac
See my tutorial page