Add alternative to find substring at the beginning of a string

This commit is contained in:
Alejandro Cervera 2023-07-28 21:31:55 -05:00
parent 26bc7e8d6c
commit 97b1534472

View file

@ -242,6 +242,27 @@ case $var in
esac
```
**Using printf builtin:**
**Example function:**
```sh
startswith() {
if [ "$(printf "%.${#1}s" "$2")" = "$1" ]; then
return 0
else
return 1
fi
}
```
**Example usage:**
```sh
$ startswith 'fo' 'foo'
```
## Check if string ends with sub-string
**Using a case statement:**