Check for substring in shell


Note to myself 🙂

Unlike bash, sh does not support  easy way of checking for substrings:

if [[ $IMAGE_TAG == *alpine* ]]

 

Solution

#!/bin/sh
IMAGE_TAG_SUFFIX=-alpine-beta

echo $IMAGE_TAG_SUFFIX | grep -i ".*alpine.*" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then echo "Alpine"; else echo "Not Alpine"; fi

Leave a comment