> For the complete documentation index, see [llms.txt](https://notes.eliasnorrby.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.eliasnorrby.com/bash/substring-contains.md).

# Bash substring

Does `var` contain 'substring'?

```bash
if [ "${var#*substring}" != "${var}" ]; then
  # do stuff
fi
```

This works by using string substitution: if removing the substring from the variable results in a string different from the original, we know the latter contains the former - otherwise, it would remain unchanged.
