Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method to detect the presence of one string within another in POSIX sh is to use parameter expansion, specifically the ${parameter#word} and ${parameter##word} forms.

To check if a string "substring" is contained within another string "string":

if [ "${string#*$substring*}" != "$string" ]; then
    echo "substring is present in string"
fi

Explanation:

${string#*$substring*} removes the substring and everything before it from the variable string. If the substring is not present, it returns the original value of string.

If the resulting value is not equal to the original string value, then that means the substring was found and the condition is true.