I'm currently refreshing my shell scripting and trying to write a function which checks if string arg1
starts with string arg2
.
Following code always prints the opposite result, but I can't find my mistake.
// Update:
Based on comments, here is the now working solution:
#!/usr/bin/env bash
## TRUE if arg1 starts with arg2, otherwise FALSE
## @param string targetPhrase
## @param string searchPhrase
str_starts_with() {
case "${1}" in "${2}"*) return 0;; *) return 1;; esac;
}
## TEST CASES
a='foobar' ; b='foo' # 1
if str_starts_with "$a" "$b"; then echo '1'; else echo '0'; fi
a='f' ; b='foo' # 0
if str_starts_with "$a" "$b"; then echo '1'; else echo '0'; fi
a='bar' ; b='f' # 0
if str_starts_with "$a" "$b"; then echo '1'; else echo '0'; fi
exit 0
1
0
0