mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-08 16:52:31 +02:00
Add alternative to find substring at the beginning of a string
This commit is contained in:
parent
26bc7e8d6c
commit
97b1534472
21
README.md
21
README.md
|
@ -242,6 +242,27 @@ case $var in
|
||||||
esac
|
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
|
## Check if string ends with sub-string
|
||||||
|
|
||||||
**Using a case statement:**
|
**Using a case statement:**
|
||||||
|
|
Loading…
Reference in a new issue