From 97b15344729ca9d15518ea7141cf668226baa36e Mon Sep 17 00:00:00 2001 From: Alejandro Cervera <96702705+tricantivu@users.noreply.github.com> Date: Fri, 28 Jul 2023 21:31:55 -0500 Subject: [PATCH] Add alternative to find substring at the beginning of a string --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 8032be5..b3e5958 100644 --- a/README.md +++ b/README.md @@ -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:**