I have a string contained in a variable, and I want to extract substrings based on position relative to another substring. My solution seems to work unless the string is sent to a function as an arg. I am using the bash shell.
#!/usr/bin/bash
var0="-a check one two three"
var1="check"
function getsubstr() {
echo ${*#*"${2}"}
}
# this prints 'one two three' which is what I want
echo ${var0#*"${var1}"}
# this prints '-a one two three', not what I want.
getsubstr $var0
note that when I put echo $*
in the getsubstr
function it prints the same string as $var0
(-> '-a check one two three'), and that when I put echo $2
in the getsubstr
function is prints the same string as $var1
(-> 'check'). So, it seems to me that I'm asking to print the same substring in both circumstances.
A further conundrum is that if, instead of echo ${*#*"${2}"}
in the getsubstr
function I use echo ${*%"${2}"*}
, I get the exact same result.
Any help in understanding this behavior would be greatly appreciated.
BTW, I realize that ${*:3}
inside the getsubstr
function works to return the substring I want, but I'm trying to understand the #*<regexp>
and %<regextp>*
behavior.